Просмотр исходного кода

Refactor out common code in AutocompleteManager.

(first pass)
Shane Kilkelly 9 лет назад
Родитель
Сommit
708afedeb4

+ 14 - 34
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee

@@ -1,21 +1,13 @@
 define [
 	"ide/editor/directives/aceEditor/auto-complete/CommandManager"
 	"ide/editor/directives/aceEditor/auto-complete/EnvironmentManager"
+	"ide/editor/directives/aceEditor/auto-complete/Helpers"
 	"ace/ace"
 	"ace/ext-language_tools"
-], (CommandManager, EnvironmentManager) ->
+], (CommandManager, EnvironmentManager, Helpers) ->
 	Range = ace.require("ace/range").Range
 	aceSnippetManager = ace.require('ace/snippets').snippetManager
 
-	getLastCommandFragment = (lineUpToCursor) ->
-		if m = lineUpToCursor.match(/(\\[^\\]+)$/)
-			return m[1]
-		else
-			return null
-
-	getCommandNameFromFragment = (commandFragment) ->
-		commandFragment?.match(/\\(\w+)\{/)?[1]
-
 	class AutoCompleteManager
 		constructor: (@$scope, @editor, @element, @labelsManager, @graphics, @preamble) ->
 			@suggestionManager = new CommandManager()
@@ -48,15 +40,11 @@ define [
 			Preamble = @preamble
 			GraphicsCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
-					upToCursorRange = new Range(pos.row, 0, pos.row, pos.column)
-					lineUpToCursor = editor.getSession().getTextRange(upToCursorRange)
-					commandFragment = getLastCommandFragment(lineUpToCursor)
+					context = Helpers.getContext(editor, pos)
+					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
 					if commandFragment
 						match = commandFragment.match(/^~?\\(includegraphics(?:\[.*])?){([^}]*, *)?(\w*)/)
 						if match
-							beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999)
-							lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange)
-							needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/)
 							commandName = match[1]
 							currentArg = match[3]
 							graphicsPaths = Preamble.getGraphicsPaths()
@@ -78,15 +66,11 @@ define [
 			labelsManager = @labelsManager
 			LabelsCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
-					upToCursorRange = new Range(pos.row, 0, pos.row, pos.column)
-					lineUpToCursor = editor.getSession().getTextRange(upToCursorRange)
-					commandFragment = getLastCommandFragment(lineUpToCursor)
+					context = Helpers.getContext(editor, pos)
+					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
 					if commandFragment
 						refMatch = commandFragment.match(/^~?\\([a-z]*ref){([^}]*, *)?(\w*)/)
 						if refMatch
-							beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999)
-							lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange)
-							needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/)
 							commandName = refMatch[1]
 							currentArg = refMatch[2]
 							result = []
@@ -108,15 +92,13 @@ define [
 			references = @$scope.$root._references
 			ReferencesCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
-					upToCursorRange = new Range(pos.row, 0, pos.row, pos.column)
-					lineUpToCursor = editor.getSession().getTextRange(upToCursorRange)
-					commandFragment = getLastCommandFragment(lineUpToCursor)
+					context = Helpers.getContext(editor, pos)
+					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
 					if commandFragment
-						citeMatch = commandFragment.match(/^~?\\([a-z]*cite[a-z]*(?:\[.*])?){([^}]*, *)?(\w*)/)
+						citeMatch = commandFragment.match(
+							/^~?\\([a-z]*cite[a-z]*(?:\[.*])?){([^}]*, *)?(\w*)/
+						)
 						if citeMatch
-							beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999)
-							lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange)
-							needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/)
 							commandName = citeMatch[1]
 							previousArgs = citeMatch[2]
 							currentArg = citeMatch[3]
@@ -160,8 +142,8 @@ define [
 		onChange: (change) ->
 			cursorPosition = @editor.getCursorPosition()
 			end = change.end
-			range = new Range(end.row, 0, end.row, end.column)
-			lineUpToCursor = @editor.getSession().getTextRange(range)
+			context = Helpers.getContext(editor, end)
+			{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
 			if lineUpToCursor.match(/.*%.*/)
 				return
 			lastCharIsBackslash = lineUpToCursor.slice(-1) == "\\"
@@ -170,8 +152,6 @@ define [
 			if lastTwoChars.match(/^\\[^a-z]$/)
 				@editor?.completer?.detach?()
 				return
-			commandFragment = getLastCommandFragment(lineUpToCursor)
-			commandName = getCommandNameFromFragment(commandFragment)
 			if commandName in ['begin', 'end']
 				return
 			# Check that this change was made by us, not a collaborator
@@ -310,5 +290,5 @@ define [
 						currentLineOffset = i + 1
 						break
 				currentLine = text.slice(currentLineOffset, pos)
-				fragment = getLastCommandFragment(currentLine) or ""
+				fragment = Helpers.getLastCommandFragment(currentLine) or ""
 				return fragment

+ 33 - 0
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/Helpers.coffee

@@ -0,0 +1,33 @@
+define [
+	"ace/ace"
+	"ace/ext-language_tools"
+], () ->
+	Range = ace.require("ace/range").Range
+
+	Helpers =
+		getLastCommandFragment: (lineUpToCursor) ->
+			if m = lineUpToCursor.match(/(\\[^\\]+)$/)
+				return m[1]
+			else
+				return null
+
+		getCommandNameFromFragment: (commandFragment) ->
+			commandFragment?.match(/\\(\w+)\{/)?[1]
+
+		getContext: (editor, pos) ->
+			upToCursorRange = new Range(pos.row, 0, pos.row, pos.column)
+			lineUpToCursor = editor.getSession().getTextRange(upToCursorRange)
+			commandFragment = Helpers.getLastCommandFragment(lineUpToCursor)
+			commandName = Helpers.getCommandNameFromFragment(commandFragment)
+			beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999)
+			lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange)
+			needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/)
+			return {
+				lineUpToCursor,
+				commandFragment,
+				commandName,
+				lineBeyondCursor,
+				needsClosingBrace
+			}
+
+	return Helpers