Refactor labels autocomplete

This commit is contained in:
Shane Kilkelly 2017-05-26 14:56:38 +01:00
parent 7923450e25
commit faf0337ca9
4 changed files with 46 additions and 37 deletions

View file

@ -9,7 +9,6 @@ define [
"ide/pdf/PdfManager"
"ide/binary-files/BinaryFilesManager"
"ide/references/ReferencesManager"
"ide/editor/directives/aceEditor/labels/LabelsManager"
"ide/review-panel/ReviewPanelManager"
"ide/SafariScrollPatcher"
"ide/FeatureOnboardingController"
@ -46,7 +45,6 @@ define [
PdfManager
BinaryFilesManager
ReferencesManager
LabelsManager
ReviewPanelManager
SafariScrollPatcher
) ->
@ -120,7 +118,6 @@ 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)

View file

@ -9,7 +9,8 @@ define [
"ide/editor/directives/aceEditor/highlights/HighlightsManager"
"ide/editor/directives/aceEditor/cursor-position/CursorPositionManager"
"ide/editor/directives/aceEditor/track-changes/TrackChangesManager"
], (App, Ace, SearchBox, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager) ->
"ide/editor/directives/aceEditor/labels/LabelsManager"
], (App, Ace, SearchBox, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager, LabelsManager) ->
EditSession = ace.require('ace/edit_session').EditSession
ModeList = ace.require('ace/ext/modelist')
@ -81,7 +82,6 @@ define [
scope.name = attrs.aceEditor
autoCompleteManager = new AutoCompleteManager(scope, editor, element)
if scope.spellCheck # only enable spellcheck when explicitly required
spellCheckCache = $cacheFactory("spellCheck-#{scope.name}", {capacity: 1000})
spellCheckManager = new SpellCheckManager(scope, editor, element, spellCheckCache)
@ -89,6 +89,8 @@ define [
highlightsManager = new HighlightsManager(scope, editor, element)
cursorPositionManager = new CursorPositionManager(scope, editor, element, localStorage)
trackChangesManager = new TrackChangesManager(scope, editor, element)
labelsManager = new LabelsManager(scope, editor, element)
autoCompleteManager = new AutoCompleteManager(scope, editor, element, labelsManager)
# Prevert Ctrl|Cmd-S from triggering save dialog
editor.commands.addCommand

View file

@ -13,7 +13,7 @@ define [
return null
class AutoCompleteManager
constructor: (@$scope, @editor) ->
constructor: (@$scope, @editor, @element, @labelsManager) ->
@suggestionManager = new SuggestionManager()
@monkeyPatchAutocomplete()
@ -31,8 +31,6 @@ define [
e.oldSession.off "change", onChange
e.session.on "change", onChange
@labelsManager = @$scope.$root._labels
enable: () ->
@editor.setOptions({
enableBasicAutocompletion: true,
@ -62,8 +60,7 @@ define [
meta: "cross-reference",
score: 11000
}
labels = labelsManager.getAllLabels()
for label in labels
for label in labelsManager.getAllLabels()
result.push {
caption: "\\ref{#{label}#{if needsClosingBrace then '}' else ''}",
value: "\\ref{#{label}#{if needsClosingBrace then '}' else ''}",
@ -124,16 +121,6 @@ define [
range = new Range(end.row, 0, end.row, end.column)
lineUpToCursor = @editor.getSession().getTextRange(range)
commandFragment = getLastCommandFragment(lineUpToCursor)
# Check if user has backspaced/deleted a highlighted region of text
# and see if that contains a `\label{}`
if change.action == 'remove'
if _.any(change.lines, (line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/))
@labelsManager.scheduleLoadLabelsFromOpenDoc()
if commandFragment? and commandFragment.length > 2
if commandFragment.startsWith('\\label{')
@labelsManager.scheduleLoadLabelsFromOpenDoc()
# Check that this change was made by us, not a collaborator
# (Cursor is still one place behind)
# NOTE: this is also the case when a user backspaces over a highlighted region
@ -143,8 +130,6 @@ define [
end.column == cursorPosition.column + 1
)
if commandFragment? and commandFragment.length > 2
if commandFragment.startsWith('\\label{')
@labelsManager.scheduleLoadLabelsFromOpenDoc()
setTimeout () =>
@editor.execCommand("startAutocomplete")
, 0

View file

@ -1,30 +1,55 @@
define [
"ace/ace"
], () ->
Range = ace.require("ace/range").Range
getLastCommandFragment = (lineUpToCursor) ->
if m = lineUpToCursor.match(/(\\[^\\]+)$/)
return m[1]
else
return null
class LabelsManager
constructor: (@ide, @$scope) ->
@$scope.$root._labels = this
constructor: (@$scope, @editor, @element) ->
@state =
documents: {} # map of DocId => List[Label]
@loadLabelsTimeout = null
setTimeout(
() =>
# listen for document open
@$scope.$on 'document:opened', (e, doc) =>
setTimeout(
() =>
@scheduleLoadLabelsFromOpenDoc()
, 0
)
, 0
)
onChange = (change) =>
cursorPosition = @editor.getCursorPosition()
end = change.end
range = new Range(end.row, 0, end.row, end.column)
lineUpToCursor = @editor.getSession().getTextRange(range)
commandFragment = getLastCommandFragment(lineUpToCursor)
if change.action == 'remove'
if _.any(change.lines, (line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/))
@scheduleLoadLabelsFromOpenDoc()
if commandFragment? and commandFragment.length > 2
if commandFragment.startsWith('\\label{')
@scheduleLoadLabelsFromOpenDoc()
if (
change.action == "insert" and
end.row == cursorPosition.row and
end.column == cursorPosition.column + 1
)
if commandFragment? and commandFragment.length > 2
if commandFragment.startsWith('\\label{')
@scheduleLoadLabelsFromOpenDoc()
@editor.on "changeSession", (e) =>
e.oldSession.off "change", onChange
e.session.on "change", onChange
setTimeout(
() =>
@scheduleLoadLabelsFromOpenDoc()
, 0
)
loadLabelsFromOpenDoc: () ->
docId = @ide.editorManager.getCurrentDocId()
docText = @ide.editorManager.getCurrentDocValue()
docId = @$scope.docId
docText = @editor.getValue()
labels = []
re = /\\label\{([^\}\n\\]{0,80})\}/g
while (labelMatch = re.exec(docText)) and labels.length < 1000