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

Merge pull request #283 from sharelatex/ns-fix-package-insertion

fix package inserstion; fixes #207
Nate Stemen 8 лет назад
Родитель
Сommit
81406d1f49

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

@@ -29,15 +29,15 @@ define [
 
 		enable: () ->
 			@editor.setOptions({
-				enableBasicAutocompletion: true,
-				enableSnippets: true,
+				enableBasicAutocompletion: true
+				enableSnippets: true
 				enableLiveAutocompletion: false
 			})
 
 			commandCompleter = new CommandManager(@metadataManager)
 
 			SnippetCompleter = new EnvironmentManager()
-			PackageCompleter = new PackageManager(@metadataManager)
+			PackageCompleter = new PackageManager(@metadataManager, Helpers)
 
 			Graphics = @graphics
 			Preamble = @preamble
@@ -46,7 +46,7 @@ define [
 			GraphicsCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
 					context = Helpers.getContext(editor, pos)
-					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
+					{commandFragment, closingBrace} = context
 					if commandFragment
 						match = commandFragment.match(/^~?\\(includegraphics(?:\[.*])?){([^}]*, *)?(\w*)/)
 						if match
@@ -61,9 +61,9 @@ define [
 										path = path.slice(graphicsPath.length)
 										break
 								result.push {
-									caption: "\\#{commandName}{#{path}#{if needsClosingBrace then '}' else ''}",
-									value: "\\#{commandName}{#{path}#{if needsClosingBrace then '}' else ''}",
-									meta: "graphic",
+									caption: "\\#{commandName}{#{path}#{closingBrace}"
+									value: "\\#{commandName}{#{path}#{closingBrace}"
+									meta: "graphic"
 									score: 50
 								}
 							callback null, result
@@ -72,7 +72,7 @@ define [
 			FilesCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) =>
 					context = Helpers.getContext(editor, pos)
-					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
+					{commandFragment, closingBrace} = context
 					if commandFragment
 						match = commandFragment.match(/^\\(input|include){(\w*)/)
 						if match
@@ -83,9 +83,9 @@ define [
 								if file.id != @$scope.docId
 									path = file.path
 									result.push {
-										caption: "\\#{commandName}{#{path}#{if needsClosingBrace then '}' else ''}",
-										value: "\\#{commandName}{#{path}#{if needsClosingBrace then '}' else ''}",
-										meta: "file",
+										caption: "\\#{commandName}{#{path}#{closingBrace}"
+										value: "\\#{commandName}{#{path}#{closingBrace}"
+										meta: "file"
 										score: 50
 									}
 							callback null, result
@@ -93,7 +93,7 @@ define [
 			LabelsCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
 					context = Helpers.getContext(editor, pos)
-					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
+					{commandFragment, closingBrace} = context
 					if commandFragment
 						refMatch = commandFragment.match(/^~?\\([a-z]*ref){([^}]*, *)?(\w*)/)
 						if refMatch
@@ -102,16 +102,16 @@ define [
 							result = []
 							if commandName != 'ref' # ref is in top 100 commands
 								result.push {
-									caption: "\\#{commandName}{}",
-									snippet: "\\#{commandName}{}",
-									meta: "cross-reference",
+									caption: "\\#{commandName}{}"
+									snippet: "\\#{commandName}{}"
+									meta: "cross-reference"
 									score: 60
 								}
 							for label in metadataManager.getAllLabels()
 								result.push {
-									caption: "\\#{commandName}{#{label}#{if needsClosingBrace then '}' else ''}",
-									value: "\\#{commandName}{#{label}#{if needsClosingBrace then '}' else ''}",
-									meta: "cross-reference",
+									caption: "\\#{commandName}{#{label}#{closingBrace}"
+									value: "\\#{commandName}{#{label}#{closingBrace}"
+									meta: "cross-reference"
 									score: 50
 								}
 							callback null, result
@@ -120,7 +120,7 @@ define [
 			ReferencesCompleter =
 				getCompletions: (editor, session, pos, prefix, callback) ->
 					context = Helpers.getContext(editor, pos)
-					{lineUpToCursor, commandFragment, lineBeyondCursor, needsClosingBrace} = context
+					{commandFragment, closingBrace} = context
 					if commandFragment
 						citeMatch = commandFragment.match(
 							/^~?\\([a-z]*cite[a-z]*(?:\[.*])?){([^}]*, *)?(\w*)/
@@ -134,18 +134,18 @@ define [
 							previousArgsCaption = if previousArgs.length > 8 then "…," else previousArgs
 							result = []
 							result.push {
-								caption: "\\#{commandName}{}",
-								snippet: "\\#{commandName}{}",
-								meta: "reference",
+								caption: "\\#{commandName}{}"
+								snippet: "\\#{commandName}{}"
+								meta: "reference"
 								score: 60
 							}
 							if references.keys and references.keys.length > 0
 								references.keys.forEach (key) ->
 									if !(key in [null, undefined])
 										result.push({
-											caption: "\\#{commandName}{#{previousArgsCaption}#{key}#{if needsClosingBrace then '}' else ''}",
-											value: "\\#{commandName}{#{previousArgs}#{key}#{if needsClosingBrace then '}' else ''}",
-											meta: "reference",
+											caption: "\\#{commandName}{#{previousArgsCaption}#{key}#{closingBrace}"
+											value: "\\#{commandName}{#{previousArgs}#{key}#{closingBrace}"
+											meta: "reference"
 											score: 50
 										})
 								callback null, result
@@ -155,7 +155,7 @@ define [
 			@editor.completers = [
 				commandCompleter
 				SnippetCompleter
-                PackageCompleter
+				PackageCompleter
 				ReferencesCompleter
 				LabelsCompleter
 				GraphicsCompleter
@@ -172,7 +172,7 @@ define [
 			cursorPosition = @editor.getCursorPosition()
 			end = change.end
 			context = Helpers.getContext(@editor, end)
-			{lineUpToCursor, commandFragment, commandName, lineBeyondCursor, needsClosingBrace} = context
+			{lineUpToCursor, commandFragment} = context
 			if lineUpToCursor.match(/.*%.*/)
 				return
 			lastCharIsBackslash = lineUpToCursor.slice(-1) == "\\"

+ 2 - 1
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/Helpers.coffee

@@ -34,12 +34,13 @@ define [
 			beyondCursorRange = new Range(pos.row, pos.column, pos.row, 99999)
 			lineBeyondCursor = editor.getSession().getTextRange(beyondCursorRange)
 			needsClosingBrace = !lineBeyondCursor.match(/^[^{]*}/)
+			closingBrace = if needsClosingBrace then '}' else ''
 			return {
 				lineUpToCursor,
 				commandFragment,
 				commandName,
 				lineBeyondCursor,
-				needsClosingBrace
+				closingBrace
 			}
 
 	return Helpers

+ 6 - 3
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/PackageManager.coffee

@@ -1,4 +1,6 @@
-define () ->
+define [
+	"./Helpers"
+], (Helpers) ->
 	packages = [
 		'inputenc', 'graphicx', 'amsmath', 'geometry', 'amssymb', 'hyperref',
 		'babel', 'color', 'xcolor', 'url', 'natbib', 'fontenc', 'fancyhdr',
@@ -24,13 +26,14 @@ define () ->
 		constructor: (@metadataManager) ->
 
 		getCompletions: (editor, session, pos, prefix, callback) ->
+			{closingBrace} = Helpers.getContext(editor, pos)
 			usedPackages = Object.keys(@metadataManager.getAllPackages())
 			packageSnippets = []
 			for pkg in packages
 				if pkg not in usedPackages
 					packageSnippets.push {
-						caption: "\\usepackage{#{pkg}}"
-						snippet: "\\usepackage{#{pkg}}"
+						caption: "\\usepackage{#{pkg}#{closingBrace}"
+						snippet: "\\usepackage{#{pkg}#{closingBrace}"
 						meta: "pkg"
 					}
 

+ 5 - 10
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/top_hundred_snippets.coffee

@@ -1,32 +1,27 @@
 define -> [{
     "caption": "\\begin{}",
     "snippet": "\\begin{$1}",
-    "meta": "cmd",
+    "meta": "env",
     "score": 7.849662248028187
 }, {
     "caption": "\\begin{}[]",
     "snippet": "\\begin{$1}[$2]",
-    "meta": "cmd",
+    "meta": "env",
     "score": 7.849662248028187
 }, {
     "caption": "\\begin{}{}",
     "snippet": "\\begin{$1}{$2}",
-    "meta": "cmd",
+    "meta": "env",
     "score": 7.849662248028187
 }, {
     "caption": "\\end{}",
     "snippet": "\\end{$1}",
-    "meta": "cmd",
+    "meta": "env",
     "score": 7.847906405228455
-}, {
-    "caption": "\\usepackage{}",
-    "snippet": "\\usepackage{$1}",
-    "meta": "cmd",
-    "score": 5.427890758130527
 }, {
     "caption": "\\usepackage[]{}",
     "snippet": "\\usepackage[$1]{$2}",
-    "meta": "cmd",
+    "meta": "pkg",
     "score": 5.427890758130527
 }, {
     "caption": "\\item",