Merge pull request #667 from sharelatex/ns-dropdown-on-completion

dropdown autocomplete when command is completed
This commit is contained in:
James Allen 2018-06-22 15:59:21 +01:00 committed by GitHub
commit 962d39f056
6 changed files with 42 additions and 48 deletions

View file

@ -44,13 +44,11 @@ define [
GraphicsCompleter =
getCompletions: (editor, session, pos, prefix, callback) ->
context = Helpers.getContext(editor, pos)
{commandFragment, closingBrace} = context
{commandFragment} = Helpers.getContext(editor, pos)
if commandFragment
match = commandFragment.match(/^~?\\(includegraphics(?:\[.*])?){([^}]*, *)?(\w*)/)
if match
commandName = match[1]
currentArg = match[3]
[_, commandName, _, currentArg] = match
graphicsPaths = Preamble.getGraphicsPaths()
result = []
for graphic in Graphics.getGraphicsFiles()
@ -60,8 +58,8 @@ define [
path = path.slice(graphicsPath.length)
break
result.push {
caption: "\\#{commandName}{#{path}#{closingBrace}"
value: "\\#{commandName}{#{path}#{closingBrace}"
caption: "\\#{commandName}{#{path}}"
value: "\\#{commandName}{#{path}}"
meta: "graphic"
score: 50
}
@ -70,20 +68,18 @@ define [
metadataManager = @metadataManager
FilesCompleter =
getCompletions: (editor, session, pos, prefix, callback) =>
context = Helpers.getContext(editor, pos)
{commandFragment, closingBrace} = context
{commandFragment} = Helpers.getContext(editor, pos)
if commandFragment
match = commandFragment.match(/^\\(input|include){(\w*)/)
if match
commandName = match[1]
currentArg = match[2]
[_, commandName, currentArg] = match
result = []
for file in Files.getTeXFiles()
if file.id != @$scope.docId
path = file.path
result.push {
caption: "\\#{commandName}{#{path}#{closingBrace}"
value: "\\#{commandName}{#{path}#{closingBrace}"
caption: "\\#{commandName}{#{path}}"
value: "\\#{commandName}{#{path}}"
meta: "file"
score: 50
}
@ -91,13 +87,11 @@ define [
LabelsCompleter =
getCompletions: (editor, session, pos, prefix, callback) ->
context = Helpers.getContext(editor, pos)
{commandFragment, closingBrace} = context
{commandFragment} = Helpers.getContext(editor, pos)
if commandFragment
refMatch = commandFragment.match(/^~?\\([a-z]*ref){([^}]*, *)?(\w*)/)
if refMatch
commandName = refMatch[1]
currentArg = refMatch[2]
[_, commandName, currentArg] = refMatch
result = []
if commandName != 'ref' # ref is in top 100 commands
result.push {
@ -108,8 +102,8 @@ define [
}
for label in metadataManager.getAllLabels()
result.push {
caption: "\\#{commandName}{#{label}#{closingBrace}"
value: "\\#{commandName}{#{label}#{closingBrace}"
caption: "\\#{commandName}{#{label}}"
value: "\\#{commandName}{#{label}}"
meta: "cross-reference"
score: 50
}
@ -118,17 +112,14 @@ define [
references = @$scope.$root._references
ReferencesCompleter =
getCompletions: (editor, session, pos, prefix, callback) ->
context = Helpers.getContext(editor, pos)
{commandFragment, closingBrace} = context
{commandFragment} = Helpers.getContext(editor, pos)
if commandFragment
citeMatch = commandFragment.match(
/^~?\\([a-z]*cite[a-z]*(?:\[.*])?){([^}]*, *)?(\w*)/
)
if citeMatch
commandName = citeMatch[1]
previousArgs = citeMatch[2]
currentArg = citeMatch[3]
if previousArgs == undefined
[_, commandName, previousArgs, currentArg] = citeMatch
if !previousArgs?
previousArgs = ""
previousArgsCaption = if previousArgs.length > 8 then "…," else previousArgs
result = []
@ -140,10 +131,10 @@ define [
}
if references.keys and references.keys.length > 0
references.keys.forEach (key) ->
if !(key in [null, undefined])
if key?
result.push({
caption: "\\#{commandName}{#{previousArgsCaption}#{key}#{closingBrace}"
value: "\\#{commandName}{#{previousArgs}#{key}#{closingBrace}"
caption: "\\#{commandName}{#{previousArgsCaption}#{key}}"
value: "\\#{commandName}{#{previousArgs}#{key}}"
meta: "reference"
score: 50
})
@ -170,8 +161,7 @@ define [
onChange: (change) ->
cursorPosition = @editor.getCursorPosition()
end = change.end
context = Helpers.getContext(@editor, end)
{lineUpToCursor, commandFragment} = context
{lineUpToCursor, commandFragment} = Helpers.getContext(@editor, end)
if lineUpToCursor.match(/.*%.*/)
return
lastCharIsBackslash = lineUpToCursor.slice(-1) == "\\"
@ -188,10 +178,19 @@ define [
end.row == cursorPosition.row and
end.column == cursorPosition.column + 1
)
if (commandFragment? and commandFragment.length > 2) or lastCharIsBackslash
if commandFragment?.length > 2 or lastCharIsBackslash
setTimeout () =>
@editor.execCommand("startAutocomplete")
, 0
if (
change.action == "insert" and
change.lines[0].match(/\\(\w+){}/)?[1].match(
/(begin|end|[a-z]*ref|usepackage|[a-z]*cite[a-z]*|input|include)/
)
)
setTimeout () =>
@editor.execCommand("startAutocomplete")
, 0
monkeyPatchAutocomplete: () ->
Autocomplete = ace.require("ace/autocomplete").Autocomplete
@ -209,7 +208,7 @@ define [
# If we are in \begin{it|}, then we need to remove the trailing }
# since it will be adding in with the autocomplete of \begin{item}...
if this.completions.filterText.match(/^\\begin\{/) and nextChar == "}"
if this.completions.filterText.match(/^\\\w+{/) and nextChar == "}"
editor.session.remove(range)
# Provide our own `insertMatch` implementation.

View file

@ -154,14 +154,14 @@ define [
totalArgs = squareArgsNo + curlyArgsNo
if totalArgs == 0
completionBeforeCursor = completionBase
completionAfterCurspr = ""
completionAfterCursor = ""
else
completionBeforeCursor = completionBase + args[0]
completionAfterCursor = args.slice(1)
return {
base: base,
completion: completionBase + args,
base: base
completion: completionBase + args
completionBeforeCursor: completionBeforeCursor
completionAfterCursor: completionAfterCursor
}

View file

@ -160,14 +160,10 @@ define () ->
hasDocumentEnvironment = (text) ->
re = /^\\begin{document}/m
envs = []
iterations = 0
return re.exec(text) != null
hasBibliographyEnvironment = (text) ->
re = /^\\begin{thebibliography}/m
envs = []
iterations = 0
return re.exec(text) != null
class EnvironmentManager

View file

@ -33,14 +33,11 @@ define [
commandName = Helpers.getCommandNameFromFragment(commandFragment)
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,
closingBrace
lineBeyondCursor
}
return Helpers

View file

@ -1,6 +1,4 @@
define [
"./Helpers"
], (Helpers) ->
define [], () ->
packages = [
'inputenc', 'graphicx', 'amsmath', 'geometry', 'amssymb', 'hyperref',
'babel', 'color', 'xcolor', 'url', 'natbib', 'fontenc', 'fancyhdr',
@ -26,14 +24,13 @@ 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}#{closingBrace}"
snippet: "\\usepackage{#{pkg}#{closingBrace}"
caption: "\\usepackage{#{pkg}}"
snippet: "\\usepackage{#{pkg}}"
meta: "pkg"
}

View file

@ -73,6 +73,11 @@ define -> [{
"snippet": "\\documentclass{$1}",
"meta": "cmd",
"score": 1.4425339817971206
}, {
"caption": "\\ref{}",
"snippet": "\\ref{$1}",
"meta": "cross-reference",
"score": 0.014379554883991673
}, {
"caption": "\\frac{}{}",
"snippet": "\\frac{$1}{$2}",