mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-28 19:41:33 +02:00
WIP: basically working labels/ref autocomplete
This commit is contained in:
@@ -31,6 +31,8 @@ define [
|
||||
e.oldSession.off "change", onChange
|
||||
e.session.on "change", onChange
|
||||
|
||||
@labelsManager = @$scope.$root._labels
|
||||
|
||||
enable: () ->
|
||||
@editor.setOptions({
|
||||
enableBasicAutocompletion: true,
|
||||
@@ -40,10 +42,10 @@ define [
|
||||
|
||||
SnippetCompleter = new SnippetManager()
|
||||
|
||||
labelsState = @$scope.$root._labels
|
||||
labelsManager = @labelsManager
|
||||
LabelsCompleter =
|
||||
getCompletions: (editor, session, pos, prefxi, callback) ->
|
||||
console.log ">> [LabelsCompleter] getting completions"
|
||||
# console.log ">> [LabelsCompleter] getting completions"
|
||||
upToCursorRange = new Range(pos.row, 0, pos.row, pos.column)
|
||||
lineUpToCursor = editor.getSession().getTextRange(upToCursorRange)
|
||||
commandFragment = getLastCommandFragment(lineUpToCursor)
|
||||
@@ -61,7 +63,7 @@ define [
|
||||
meta: "cross-reference",
|
||||
score: 11000
|
||||
}
|
||||
labels = _.flatten(labels for docId, labels of labelsState.documents)
|
||||
labels = labelsManager.getAllLabels()
|
||||
for label in labels
|
||||
result.push {
|
||||
caption: "\\ref{#{label}#{if needsClosingBrace then '}' else ''}",
|
||||
@@ -118,6 +120,7 @@ define [
|
||||
})
|
||||
|
||||
onChange: (change) ->
|
||||
window.EDITOR = @editor
|
||||
cursorPosition = @editor.getCursorPosition()
|
||||
end = change.end
|
||||
# Check that this change was made by us, not a collaborator
|
||||
@@ -129,9 +132,19 @@ define [
|
||||
commandFragment = getLastCommandFragment(lineUpToCursor)
|
||||
|
||||
if commandFragment? and commandFragment.length > 2
|
||||
if commandFragment.startsWith('\\label{')
|
||||
# console.log ">> LABEL IS HERE"
|
||||
# TODO: trigger re-scan of document
|
||||
@labelsManager.scheduleLoadLabelsFromOpenDoc()
|
||||
setTimeout () =>
|
||||
@editor.execCommand("startAutocomplete")
|
||||
, 0
|
||||
else
|
||||
if change.action == 'remove'
|
||||
if _.any(change.lines, (line) -> line.match(/\\label{.*}/))
|
||||
# console.log ">> a label has been removed"
|
||||
# TODO: trigger removal of label
|
||||
@labelsManager.scheduleLoadLabelsFromOpenDoc()
|
||||
|
||||
monkeyPatchAutocomplete: () ->
|
||||
Autocomplete = ace.require("ace/autocomplete").Autocomplete
|
||||
|
||||
@@ -2,37 +2,49 @@ define [
|
||||
], () ->
|
||||
class LabelsManager
|
||||
constructor: (@ide, @$scope) ->
|
||||
@$scope.$root._labels = this
|
||||
|
||||
@$scope.$root._labels = @state =
|
||||
@state =
|
||||
documents: {}
|
||||
|
||||
window.STATE = @state
|
||||
@loadLabelsTimeout = null
|
||||
|
||||
setTimeout(
|
||||
(self) ->
|
||||
self.$scope.$on 'document:opened', (e, doc) ->
|
||||
console.log ">> [LabelsManager] document opened"
|
||||
# console.log ">> [LabelsManager] document opened"
|
||||
setTimeout(
|
||||
(self, doc) ->
|
||||
self.loadLabelsFromDoc(doc)
|
||||
(self) ->
|
||||
self.loadLabelsFromOpenDoc()
|
||||
, 1000
|
||||
, self
|
||||
, doc
|
||||
)
|
||||
, 0
|
||||
this
|
||||
)
|
||||
|
||||
loadLabelsFromDoc: (doc) ->
|
||||
docId = doc.doc_id
|
||||
console.log ">> [LabelsMangager] loading labels", docId
|
||||
docText = doc._doc.getText()
|
||||
loadLabelsFromOpenDoc: () ->
|
||||
docId = @ide.editorManager.getCurrentDocId()
|
||||
# console.log ">> [LabelsMangager] loading labels", docId
|
||||
docText = @ide.editorManager.getCurrentDocValue()
|
||||
labels = []
|
||||
re = /\\label{(.*)}/g
|
||||
while labelMatch = re.exec(docText)
|
||||
labels.push(labelMatch[1])
|
||||
if labelMatch[1]
|
||||
labels.push(labelMatch[1])
|
||||
@state.documents[docId] = labels
|
||||
console.log ">> [LabelsMangager] success, loaded labels", docId, labels
|
||||
# console.log ">> [LabelsMangager] success, loaded labels", docId, labels
|
||||
|
||||
scheduleLoadLabelsFromOpenDoc: () ->
|
||||
if @loadLabelsTimeout
|
||||
clearTimeout(@loadLabelsTimeout)
|
||||
@loadLabelsTimeout = setTimeout(
|
||||
(self) ->
|
||||
# console.log ">> trigger timeout"
|
||||
self.loadLabelsFromOpenDoc()
|
||||
, 500
|
||||
, this
|
||||
)
|
||||
|
||||
getAllLabels: () ->
|
||||
_.flatten(labels for docId, labels of @state.documents)
|
||||
|
||||
Reference in New Issue
Block a user