mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #572 from sharelatex/ns-autocomplete
Adding default list of commands to autocomplete
This commit is contained in:
commit
2e005fd39a
3 changed files with 131 additions and 34 deletions
|
@ -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
|
||||||
|
|
|
@ -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,33 +159,35 @@ 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
|
||||||
caption = "\\#{command[0]}"
|
if command[0] not in rawCommands
|
||||||
score = if caption == prefix then 99 else 50
|
caption = "\\#{command[0]}"
|
||||||
snippet = caption
|
score = if caption == prefix then 99 else 50
|
||||||
i = 1
|
snippet = caption
|
||||||
_.times command[1], () ->
|
i = 1
|
||||||
snippet += "[${#{i}}]"
|
_.times command[1], () ->
|
||||||
caption += "[]"
|
snippet += "[${#{i}}]"
|
||||||
i++
|
caption += "[]"
|
||||||
_.times command[2], () ->
|
i++
|
||||||
snippet += "{${#{i}}}"
|
_.times command[2], () ->
|
||||||
caption += "{}"
|
snippet += "{${#{i}}}"
|
||||||
i++
|
caption += "{}"
|
||||||
completions.push {
|
i++
|
||||||
caption: caption
|
completions.push {
|
||||||
snippet: snippet
|
caption: caption
|
||||||
meta: "cmd"
|
snippet: snippet
|
||||||
score: score
|
meta: "cmd"
|
||||||
}
|
score: score
|
||||||
|
}
|
||||||
|
completions = completions.concat staticCommands
|
||||||
|
|
||||||
callback null, completions
|
callback(null, completions)
|
||||||
|
|
||||||
loadCommandsFromDoc: (doc) ->
|
loadCommandsFromDoc: (doc) ->
|
||||||
parser = new Parser(doc)
|
parser = new Parser(doc)
|
|
@ -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)
|
||||||
result.push {name: match[1], whitespace: match[2]}
|
if match[1] not in environmentNames
|
||||||
iterations += 1
|
result.push {name: match[1], whitespace: match[2]}
|
||||||
if iterations >= 1000
|
iterations += 1
|
||||||
return result
|
if iterations >= 1000
|
||||||
|
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
|
Loading…
Reference in a new issue