define(function(require,exports,module){let EditorManager=brackets.getModule("editor/EditorManager"),ScopeManager=brackets.getModule("JSUtils/ScopeManager"),Session=brackets.getModule("JSUtils/Session"),MessageIds=JSON.parse(brackets.getModule("text!JSUtils/MessageIds.json")),Editor=brackets.getModule("editor/Editor").Editor,session=null;function initializeSession(editor){session=new Session(editor)}function getRefs(fileInfo,offset){return ScopeManager.postMessage({type:MessageIds.TERN_REFS,fileInfo:fileInfo,offset:offset}),ScopeManager.addPendingRequest(fileInfo.name,offset,MessageIds.TERN_REFS)}function requestFindRefs(session,document,offset){if(!document||!session)return;let path=document.file.fullPath,fileInfo,ternPromise;return{promise:getRefs({type:MessageIds.TERN_FILE_INFO_TYPE_FULL,name:path,offsetLines:0,text:ScopeManager.filterText(session.getJavascriptText())},offset)}}const HIGHLIGHT_REFS_MARKER="JS_REFS";function _handleHighLightRefs(editor,refsResp){refsResp&&refsResp.references&&refsResp.references.refs&&editor.operation(function(){for(let ref of refsResp.references.refs)editor.document.file.fullPath.endsWith(ref.file)&&editor.markText(HIGHLIGHT_REFS_MARKER,ref.start,ref.end,Editor.getMarkOptionMatchingRefs())})}function _hasASingleCursor(editor){let selections=editor.getSelections();if(selections.length>1)return!1;let start=selections[0].start,end=selections[0].end;return start.line===end.line&&start.ch===end.ch}let allowedHighlightTypes=["def","variable","variable-2","variable-3","property"],lastHighlightToken={};function _cursorActivity(_evt,editor){if("javascript"!==editor.getModeForSelection())return;if(!_hasASingleCursor(editor))return void editor.clearAllMarks(HIGHLIGHT_REFS_MARKER);let token=editor.getToken();if(lastHighlightToken===token)return;if(editor.clearAllMarks(HIGHLIGHT_REFS_MARKER),lastHighlightToken=token,!allowedHighlightTypes.includes(token.type))return;let offset=session.getOffset();requestFindRefs(session,session.editor.document,offset).promise.done(response=>{_handleHighLightRefs(editor,response)}).fail(function(err){console.error("find references failed with: ",err)})}function _activeEditorChanged(_evt,current,previous){previous&&previous.off("cursorActivity.highlightRefs"),current&&(current.off("cursorActivity.highlightRefs"),current.on("cursorActivity.highlightRefs",_cursorActivity),initializeSession(current),_cursorActivity(_evt,current))}EditorManager.on("activeEditorChange",_activeEditorChanged),exports.HIGHLIGHT_REFS_MARKER=HIGHLIGHT_REFS_MARKER});