Shane Kilkelly 9 лет назад
Родитель
Сommit
faf0337ca9

+ 0 - 3
services/web/public/coffee/ide.coffee

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

+ 4 - 2
services/web/public/coffee/ide/editor/directives/aceEditor.coffee

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

+ 2 - 17
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee

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

+ 40 - 15
services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee

@@ -1,30 +1,55 @@
 define [
 define [
+	"ace/ace"
 ], () ->
 ], () ->
+	Range = ace.require("ace/range").Range
+
+	getLastCommandFragment = (lineUpToCursor) ->
+		if m = lineUpToCursor.match(/(\\[^\\]+)$/)
+			return m[1]
+		else
+			return null
 
 
 	class LabelsManager
 	class LabelsManager
-		constructor: (@ide, @$scope) ->
-			@$scope.$root._labels = this
+		constructor: (@$scope, @editor, @element) ->
 
 
 			@state =
 			@state =
 				documents: {} # map of DocId => List[Label]
 				documents: {} # map of DocId => List[Label]
 
 
 			@loadLabelsTimeout = null
 			@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: () ->
 		loadLabelsFromOpenDoc: () ->
-			docId = @ide.editorManager.getCurrentDocId()
-			docText = @ide.editorManager.getCurrentDocValue()
+			docId = @$scope.docId
+			docText = @editor.getValue()
 			labels = []
 			labels = []
 			re = /\\label\{([^\}\n\\]{0,80})\}/g
 			re = /\\label\{([^\}\n\\]{0,80})\}/g
 			while (labelMatch = re.exec(docText)) and labels.length < 1000
 			while (labelMatch = re.exec(docText)) and labels.length < 1000