forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocumentHighlight.js
More file actions
1 lines (1 loc) · 2.69 KB
/
Copy pathDocumentHighlight.js
File metadata and controls
1 lines (1 loc) · 2.69 KB
1
define(function(require,exports,module){const EditorManager=require("editor/EditorManager"),Editor=require("editor/Editor").Editor,HIGHLIGHT_MARKER="lsp-document-highlight",REQUEST_DEBOUNCE_MS=150,SKIP_TOKEN_TYPES=["comment","string","number"];let initialized=!1;const registeredClients=[];let lastTokenKey=null,debounceTimer=null,requestSeq=0;function _clientForEditor(editor){if(!editor)return null;const langId=editor.document.getLanguage().getId();for(let i=0;i<registeredClients.length;i++){const c=registeredClients[i];if(c.capabilities&&c.capabilities.documentHighlightProvider&&-1!==c.languages.indexOf(langId))return c}return null}function _hasSingleCursor(editor){const selections=editor.getSelections();if(selections.length>1)return!1;const start=selections[0].start,end=selections[0].end;return start.line===end.line&&start.ch===end.ch}function _cursorActivity(_evt,editor){const client=_clientForEditor(editor);if(!client)return;if(!_hasSingleCursor(editor))return editor.clearAllMarks(HIGHLIGHT_MARKER),void(lastTokenKey=null);const pos=editor.getCursorPos(),token=editor.getToken(pos),tokenKey=pos.line+":"+(token?token.start+":"+token.string:pos.ch);if(tokenKey===lastTokenKey)return;if(editor.clearAllMarks(HIGHLIGHT_MARKER),lastTokenKey=tokenKey,!token||!token.string||!/[\w$]/.test(token.string)||token.type&&-1!==SKIP_TOKEN_TYPES.indexOf(token.type))return;debounceTimer&&clearTimeout(debounceTimer);const seq=++requestSeq;debounceTimer=setTimeout(function(){debounceTimer=null,client.documentHighlight({filePath:editor.document.file._path,cursorPos:pos}).done(function(highlights){seq===requestSeq&&EditorManager.getActiveEditor()===editor&&highlights&&highlights.length&&editor.operation(function(){highlights.forEach(function(h){h&&h.range&&editor.markText(HIGHLIGHT_MARKER,{line:h.range.start.line,ch:h.range.start.character},{line:h.range.end.line,ch:h.range.end.character},Editor.getMarkOptionMatchingRefs())})})}).fail(function(){})},REQUEST_DEBOUNCE_MS)}function _activeEditorChanged(evt,current,previous){previous&&previous.off("cursorActivity.lspHighlight"),current&&(current.off("cursorActivity.lspHighlight"),current.on("cursorActivity.lspHighlight",_cursorActivity),lastTokenKey=null,_cursorActivity(evt,current))}function init(){if(initialized)return;initialized=!0,EditorManager.on("activeEditorChange",_activeEditorChanged);const editor=EditorManager.getActiveEditor();editor&&_activeEditorChanged(null,editor,null)}function registerClient(client){-1===registeredClients.indexOf(client)&®isteredClients.push(client);const editor=EditorManager.getActiveEditor();editor&&(lastTokenKey=null,_cursorActivity(null,editor))}exports.init=init,exports.registerClient=registerClient,exports.HIGHLIGHT_MARKER=HIGHLIGHT_MARKER});