Merge pull request #572 from sharelatex/ns-autocomplete

Adding default list of commands to autocomplete
This commit is contained in:
James Allen 2017-08-29 15:43:24 +02:00 committed by GitHub
commit 2e005fd39a
3 changed files with 131 additions and 34 deletions

View file

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

View file

@ -1,4 +1,75 @@
define [], () -> 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 class Parser
constructor: (@doc, @prefix) -> constructor: (@doc, @prefix) ->
@ -88,13 +159,14 @@ define [], () ->
else else
return false return false
class SuggestionManager class CommandManager
getCompletions: (editor, session, pos, prefix, callback) -> getCompletions: (editor, session, pos, prefix, callback) ->
doc = session.getValue() doc = session.getValue()
parser = new Parser(doc, prefix) parser = new Parser(doc, prefix)
commands = parser.parse() commands = parser.parse()
completions = [] completions = []
for command in commands for command in commands
if command[0] not in rawCommands
caption = "\\#{command[0]}" caption = "\\#{command[0]}"
score = if caption == prefix then 99 else 50 score = if caption == prefix then 99 else 50
snippet = caption snippet = caption
@ -113,8 +185,9 @@ define [], () ->
meta: "cmd" meta: "cmd"
score: score score: score
} }
completions = completions.concat staticCommands
callback null, completions callback(null, completions)
loadCommandsFromDoc: (doc) -> loadCommandsFromDoc: (doc) ->
parser = new Parser(doc) parser = new Parser(doc)

View file

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