Jelajahi Sumber

Merge pull request #572 from sharelatex/ns-autocomplete

Adding default list of commands to autocomplete
James Allen 9 tahun lalu
induk
melakukan
2e005fd39a

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

@@ -1,9 +1,9 @@
 define [
-	"ide/editor/directives/aceEditor/auto-complete/SuggestionManager"
-	"ide/editor/directives/aceEditor/auto-complete/SnippetManager"
+	"ide/editor/directives/aceEditor/auto-complete/CommandManager"
+	"ide/editor/directives/aceEditor/auto-complete/EnvironmentManager"
 	"ace/ace"
 	"ace/ext-language_tools"
-], (SuggestionManager, SnippetManager) ->
+], (CommandManager, EnvironmentManager) ->
 	Range = ace.require("ace/range").Range
 	aceSnippetManager = ace.require('ace/snippets').snippetManager
 
@@ -18,7 +18,7 @@ define [
 
 	class AutoCompleteManager
 		constructor: (@$scope, @editor, @element, @labelsManager, @graphics, @preamble) ->
-			@suggestionManager = new SuggestionManager()
+			@suggestionManager = new CommandManager()
 
 			@monkeyPatchAutocomplete()
 
@@ -42,7 +42,7 @@ define [
 				enableLiveAutocompletion: false
 			})
 
-			SnippetCompleter = new SnippetManager()
+			SnippetCompleter = new EnvironmentManager()
 
 			Graphics = @graphics
 			Preamble = @preamble

+ 94 - 21
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SuggestionManager.coffee → services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/CommandManager.coffee

@@ -1,4 +1,75 @@
 define [], () ->
+	noArgumentCommands = [
+		'item', 'hline', 'lipsum', 'centering', 'noindent', 'textwidth', 'draw',
+		'maketitle', 'newpage', 'verb', 'bibliography', 'fi', 'hfill', 'par',
+		'in', 'sum', 'cdot', 'alpha', 'ldots', 'else', 'linewidth', 'left',
+		'right', 'today', 'clearpage', 'newline', 'endinput', 'mu',
+		'tableofcontents', 'vfill', 'bigskip', 'fill', 'cleardoublepage'
+	]
+	singleArgumentCommands = [
+		'chapter', 'usepackage', 'section', 'label', 'textbf', 'subsection',
+		'vspace', 'cite', 'textit', 'documentclass', 'includegraphics', 'input',
+		'emph','caption', 'ref', 'title', 'author', 'texttt', 'include',
+		'hspace', 'bibitem', 'url', 'large', 'subsubsection', 'textsc', 'date',
+		'footnote', 'small', 'thanks', 'underline', 'graphicspath', 'pageref',
+		'section*', 'subsection*', 'subsubsection*', 'sqrt', 'text',
+		'normalsize', 'Large', 'paragraph', 'pagestyle', 'thispagestyle',
+		'bibliographystyle'
+	]
+	doubleArgumentCommands = [
+		'newcommand', 'frac', 'renewcommand', 'setlength', 'href', 'newtheorem'
+	]
+	tripleArgumentCommands = [
+		'addcontentsline', 'newacronym', 'multicolumn'
+	]
+	special = ['LaTeX', 'TeX']
+
+	rawCommands = [].concat(
+						noArgumentCommands,
+						singleArgumentCommands,
+						doubleArgumentCommands,
+						tripleArgumentCommands,
+						special
+					)
+
+	noArgumentCommands = for cmd in noArgumentCommands
+		{
+			caption: "\\#{cmd}"
+			snippet: "\\#{cmd}"
+			meta: "cmd"
+		}
+	singleArgumentCommands = for cmd in singleArgumentCommands
+		{
+			caption: "\\#{cmd}{}"
+			snippet: "\\#{cmd}{$1}"
+			meta: "cmd"
+		}
+	doubleArgumentCommands = for cmd in doubleArgumentCommands
+		{
+			caption: "\\#{cmd}{}{}"
+			snippet: "\\#{cmd}{$1}{$2}"
+			meta: "cmd"
+		}
+	tripleArgumentCommands = for cmd in tripleArgumentCommands
+		{
+			caption: "\\#{cmd}{}{}{}"
+			snippet: "\\#{cmd}{$1}{$2}{$3}"
+			meta: "cmd"
+		}
+	special = for cmd in special
+			{
+				caption: "\\#{cmd}{}"
+				snippet: "\\#{cmd}{}"
+				meta: "cmd"
+			}
+
+	staticCommands = [].concat(
+						noArgumentCommands,
+						singleArgumentCommands,
+						doubleArgumentCommands,
+						tripleArgumentCommands,
+						special
+					)
 
 	class Parser
 		constructor: (@doc, @prefix) ->
@@ -88,33 +159,35 @@ define [], () ->
 			else
 				return false
 
-	class SuggestionManager
+	class CommandManager
 		getCompletions: (editor, session, pos, prefix, callback) ->
 			doc = session.getValue()
 			parser = new Parser(doc, prefix)
 			commands = parser.parse()
 			completions = []
 			for command in commands
-				caption = "\\#{command[0]}"
-				score = if caption == prefix then 99 else 50
-				snippet = caption
-				i = 1
-				_.times command[1], () ->
-					snippet += "[${#{i}}]"
-					caption += "[]"
-					i++
-				_.times command[2], () ->
-					snippet += "{${#{i}}}"
-					caption += "{}"
-					i++
-				completions.push {
-					caption: caption
-					snippet: snippet
-					meta: "cmd"
-					score: score
-				}
-
-			callback null, completions
+				if command[0] not in rawCommands
+					caption = "\\#{command[0]}"
+					score = if caption == prefix then 99 else 50
+					snippet = caption
+					i = 1
+					_.times command[1], () ->
+						snippet += "[${#{i}}]"
+						caption += "[]"
+						i++
+					_.times command[2], () ->
+						snippet += "{${#{i}}}"
+						caption += "{}"
+						i++
+					completions.push {
+						caption: caption
+						snippet: snippet
+						meta: "cmd"
+						score: score
+					}
+			completions = completions.concat staticCommands
+
+			callback(null, completions)
 
 		loadCommandsFromDoc: (doc) ->
 			parser = new Parser(doc)

+ 33 - 9
services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SnippetManager.coffee → services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/EnvironmentManager.coffee

@@ -6,9 +6,25 @@ define () ->
 		"gather", "gather*",
 		"multline", "multline*",
 		"split",
-		"verbatim"
+		"verbatim",
+		"quote",
+		"center"
 	]
 
+	snippetNames = [
+		"array",
+		"figure",
+		"tabular",
+		"table",
+		"list",
+		"enumerate",
+		"itemize",
+		"frame",
+		"thebibliography"
+	]
+
+	environmentNames = snippetNames.concat environments
+
 	staticSnippets = for env in environments
 		{
 			caption: "\\begin{#{env}}..."
@@ -95,9 +111,17 @@ define () ->
 			\\end{frame}
 		"""
 		meta: "env"
+	}, {
+		caption: "\\begin{thebibliography}..."
+		snippet: """
+			\\begin{thebibliography}{$1}
+			\\bibitem{$2}
+			$3
+			\\end{thebibliography}
+		"""
+		meta: "env"
 	}]
 
-
 	parseCustomEnvironments = (text) ->
 		re = /^\\newenvironment{(\w+)}.*$/gm
 		result = []
@@ -109,19 +133,19 @@ define () ->
 				return result
 		return result
 
-
 	parseBeginCommands = (text) ->
 		re = /^\\begin{(\w+)}.*\n([\t ]*).*$/gm
 		result = []
 		iterations = 0
 		while match = re.exec(text)
-			result.push {name: match[1], whitespace: match[2]}
-			iterations += 1
-			if iterations >= 1000
-				return result
+			if match[1] not in environmentNames
+				result.push {name: match[1], whitespace: match[2]}
+				iterations += 1
+				if iterations >= 1000
+					return result
 		return result
 
-	class SnippetManager
+	class EnvironmentManager
 		getCompletions: (editor, session, pos, prefix, callback) ->
 			docText = session.getValue()
 			customEnvironments = parseCustomEnvironments(docText)
@@ -156,4 +180,4 @@ define () ->
 			)
 			callback null, snippets
 
-	return SnippetManager
+	return EnvironmentManager