WIP: autocomplete for label/ref

This commit is contained in:
Shane Kilkelly 2017-05-24 10:07:14 +01:00
parent 76b8fecde9
commit c43a2087d7
4 changed files with 74 additions and 1 deletions

View file

@ -9,6 +9,7 @@ define [
"ide/pdf/PdfManager" "ide/pdf/PdfManager"
"ide/binary-files/BinaryFilesManager" "ide/binary-files/BinaryFilesManager"
"ide/references/ReferencesManager" "ide/references/ReferencesManager"
"ide/labels/LabelsManager"
"ide/review-panel/ReviewPanelManager" "ide/review-panel/ReviewPanelManager"
"ide/SafariScrollPatcher" "ide/SafariScrollPatcher"
"ide/FeatureOnboardingController" "ide/FeatureOnboardingController"
@ -45,6 +46,7 @@ define [
PdfManager PdfManager
BinaryFilesManager BinaryFilesManager
ReferencesManager ReferencesManager
LabelsManager
ReviewPanelManager ReviewPanelManager
SafariScrollPatcher SafariScrollPatcher
) -> ) ->
@ -118,6 +120,7 @@ define [
ide.$scope = $scope ide.$scope = $scope
ide.referencesSearchManager = new ReferencesManager(ide, $scope) ide.referencesSearchManager = new ReferencesManager(ide, $scope)
ide.labelsSearchManager = new LabelsManager(ide, $scope)
ide.connectionManager = new ConnectionManager(ide, $scope) ide.connectionManager = new ConnectionManager(ide, $scope)
ide.fileTreeManager = new FileTreeManager(ide, $scope) ide.fileTreeManager = new FileTreeManager(ide, $scope)
ide.editorManager = new EditorManager(ide, $scope) ide.editorManager = new EditorManager(ide, $scope)

View file

@ -35,6 +35,7 @@ define [
@doc?.attachToAce(@ace) @doc?.attachToAce(@ace)
editorDoc = @ace.getSession().getDocument() editorDoc = @ace.getSession().getDocument()
editorDoc.on "change", @_checkConsistency editorDoc.on "change", @_checkConsistency
@ide.$scope.$emit 'document:opened', @doc
detachFromAce: () -> detachFromAce: () ->
@doc?.detachFromAce() @doc?.detachFromAce()

View file

@ -40,6 +40,37 @@ define [
SnippetCompleter = new SnippetManager() 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 references = @$scope.$root._references
ReferencesCompleter = ReferencesCompleter =
getCompletions: (editor, session, pos, prefix, callback) -> getCompletions: (editor, session, pos, prefix, callback) ->
@ -78,7 +109,7 @@ define [
else else
callback null, result callback null, result
@editor.completers = [@suggestionManager, SnippetCompleter, ReferencesCompleter] @editor.completers = [@suggestionManager, SnippetCompleter, ReferencesCompleter, LabelsCompleter]
disable: () -> disable: () ->
@editor.setOptions({ @editor.setOptions({

View file

@ -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)