diff --git a/services/web/public/coffee/ide.coffee b/services/web/public/coffee/ide.coffee index dabf166578..54164c2659 100644 --- a/services/web/public/coffee/ide.coffee +++ b/services/web/public/coffee/ide.coffee @@ -9,6 +9,7 @@ define [ "ide/pdf/PdfManager" "ide/binary-files/BinaryFilesManager" "ide/references/ReferencesManager" + "ide/labels/LabelsManager" "ide/review-panel/ReviewPanelManager" "ide/SafariScrollPatcher" "ide/FeatureOnboardingController" @@ -45,6 +46,7 @@ define [ PdfManager BinaryFilesManager ReferencesManager + LabelsManager ReviewPanelManager SafariScrollPatcher ) -> @@ -118,6 +120,7 @@ define [ ide.$scope = $scope ide.referencesSearchManager = new ReferencesManager(ide, $scope) + ide.labelsSearchManager = new LabelsManager(ide, $scope) ide.connectionManager = new ConnectionManager(ide, $scope) ide.fileTreeManager = new FileTreeManager(ide, $scope) ide.editorManager = new EditorManager(ide, $scope) diff --git a/services/web/public/coffee/ide/editor/Document.coffee b/services/web/public/coffee/ide/editor/Document.coffee index a321b85049..d1aa883734 100644 --- a/services/web/public/coffee/ide/editor/Document.coffee +++ b/services/web/public/coffee/ide/editor/Document.coffee @@ -35,6 +35,7 @@ define [ @doc?.attachToAce(@ace) editorDoc = @ace.getSession().getDocument() editorDoc.on "change", @_checkConsistency + @ide.$scope.$emit 'document:opened', @doc detachFromAce: () -> @doc?.detachFromAce() diff --git a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee index c5b37d1f3b..c7f83bdf51 100644 --- a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee +++ b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee @@ -40,6 +40,37 @@ define [ SnippetCompleter = new SnippetManager() + labelsState = @$scope.$root._labels + LabelsCompleter = + getCompletions: (editor, session, pos, prefxi, callback) -> + console.log ">> [LabelsCompleter] getting completions" + upToCursorRange = new Range(pos.row, 0, pos.row, pos.column) + lineUpToCursor = editor.getSession().getTextRange(upToCursorRange) + commandFragment = getLastCommandFragment(lineUpToCursor) + if commandFragment + refMatch = commandFragment.match(/^~?\\ref{([^}]*, *)?(\w*)/) + if refMatch + beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999) + lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange) + needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/) + currentArg = refMatch[1] + result = [] + result.push { + caption: "\\ref{}", + snippet: "\\ref{}", + meta: "cross-reference", + score: 11000 + } + labels = _.flatten(labels for docId, labels of labelsState.documents) + for label in labels + result.push { + caption: "\\ref{#{label}#{if needsClosingBrace then '}' else ''}", + value: "\\ref{#{label}#{if needsClosingBrace then '}' else ''}", + meta: "cross-reference", + score: 10000 + } + callback null, result + references = @$scope.$root._references ReferencesCompleter = getCompletions: (editor, session, pos, prefix, callback) -> @@ -78,7 +109,7 @@ define [ else callback null, result - @editor.completers = [@suggestionManager, SnippetCompleter, ReferencesCompleter] + @editor.completers = [@suggestionManager, SnippetCompleter, ReferencesCompleter, LabelsCompleter] disable: () -> @editor.setOptions({ diff --git a/services/web/public/coffee/ide/labels/LabelsManager.coffee b/services/web/public/coffee/ide/labels/LabelsManager.coffee new file mode 100644 index 0000000000..c56f2f7793 --- /dev/null +++ b/services/web/public/coffee/ide/labels/LabelsManager.coffee @@ -0,0 +1,38 @@ +define [ +], () -> + class LabelsManager + constructor: (@ide, @$scope) -> + + @$scope.$root._labels = @state = + documents: {} + + window.STATE = @state + + setTimeout( + (self) -> + self.$scope.$on 'document:opened', (e, doc) -> + console.log ">> [LabelsManager] document opened" + setTimeout( + (self, doc) -> + self.loadLabelsFromDoc(doc) + , 1000 + , self + , doc + ) + , 0 + this + ) + + loadLabelsFromDoc: (doc) -> + docId = doc.doc_id + console.log ">> [LabelsMangager] loading labels", docId + docText = doc._doc.getText() + labels = [] + re = /\\label{(.*)}/g + while labelMatch = re.exec(docText) + labels.push(labelMatch[1]) + @state.documents[docId] = labels + console.log ">> [LabelsMangager] success, loaded labels", docId, labels + + getAllLabels: () -> + _.flatten(labels for docId, labels of @state.documents)