Refactor, take indentation into account.

This commit is contained in:
Shane Kilkelly 2016-03-17 15:27:20 +00:00
parent 229ced6f2f
commit 53b46e42cd

View file

@ -97,46 +97,44 @@ define () ->
meta: "env" meta: "env"
}] }]
CUSTOM_ENVIRONMENT_REGEX = /^\\newenvironment{(\w+)}.*$/gm
parseCustomEnvironmentNames = (text) -> parseCustomEnvironments = (text) ->
names = [] re = /^\\newenvironment{(\w+)}.*$/gm
result = []
iterations = 0 iterations = 0
while match = CUSTOM_ENVIRONMENT_REGEX.exec(text) while match = re.exec(text)
names.push match[1] result.push {name: match[1], whitespace: null}
iterations += 1 iterations += 1
if iterations >= 1000 if iterations >= 1000
return names return result
return names return result
BEGIN_COMMAND_REGEX = /^\\begin{(\w+)}.*$/gm
parseBeginCommandNames = (text) -> parseBeginCommands = (text) ->
names = [] re = /^\\begin{(\w+)}.*\n([\t ]*).*$/gm
result = []
iterations = 0 iterations = 0
while match = BEGIN_COMMAND_REGEX.exec(text) while match = re.exec(text)
names.push match[1] result.push {name: match[1], whitespace: match[2]}
iterations += 1 iterations += 1
if iterations >= 1000 if iterations >= 1000
return names return result
return names return result
class SnippetManager class SnippetManager
getCompletions: (editor, session, pos, prefix, callback) -> getCompletions: (editor, session, pos, prefix, callback) ->
# console.log ">> get snippet completions", editor, session, pos, prefix
docText = session.getValue() docText = session.getValue()
customEnvironmentNames = parseCustomEnvironmentNames(docText) customEnvironments = parseCustomEnvironments(docText)
beginCommandNames = parseBeginCommandNames(docText) beginCommands = parseBeginCommands(docText)
# console.log customEnvironmentNames parsedItems = _.union(customEnvironments, beginCommands)
parsedNames = _.union(customEnvironmentNames, beginCommandNames)
snippets = staticSnippets.concat( snippets = staticSnippets.concat(
parsedNames.map (name) -> parsedItems.map (item) ->
{ {
caption: "\\begin{#{name}}..." caption: "\\begin{#{item.name}}..."
snippet: """ snippet: """
\\begin{#{name}} \\begin{#{item.name}}
$1 #{item.whitespace || ''}$1
\\end{#{name}} \\end{#{item.name}}
""" """
meta: "env" meta: "env"
} }
@ -144,10 +142,10 @@ define () ->
# arguably these `end` commands shouldn't be here, as they're not snippets # arguably these `end` commands shouldn't be here, as they're not snippets
# but this is where we have access to the `begin` environment names # but this is where we have access to the `begin` environment names
# *shrug* # *shrug*
parsedNames.map (name) -> parsedItems.map (item) ->
{ {
caption: "\\end{#{name}}" caption: "\\end{#{item.name}}"
value: "\\end{#{name}}" value: "\\end{#{item.name}}"
meta: "env" meta: "env"
} }
) )