From 53b46e42cde5d6cf302849cbeb5c5eac836d897d Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 17 Mar 2016 15:27:20 +0000 Subject: [PATCH] Refactor, take indentation into account. --- .../auto-complete/SnippetManager.coffee | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SnippetManager.coffee b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SnippetManager.coffee index dce8f96d0e..3b3d4baf3b 100644 --- a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SnippetManager.coffee +++ b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/SnippetManager.coffee @@ -97,46 +97,44 @@ define () -> meta: "env" }] - CUSTOM_ENVIRONMENT_REGEX = /^\\newenvironment{(\w+)}.*$/gm - parseCustomEnvironmentNames = (text) -> - names = [] + parseCustomEnvironments = (text) -> + re = /^\\newenvironment{(\w+)}.*$/gm + result = [] iterations = 0 - while match = CUSTOM_ENVIRONMENT_REGEX.exec(text) - names.push match[1] + while match = re.exec(text) + result.push {name: match[1], whitespace: null} iterations += 1 if iterations >= 1000 - return names - return names + return result + return result - BEGIN_COMMAND_REGEX = /^\\begin{(\w+)}.*$/gm - parseBeginCommandNames = (text) -> - names = [] + parseBeginCommands = (text) -> + re = /^\\begin{(\w+)}.*\n([\t ]*).*$/gm + result = [] iterations = 0 - while match = BEGIN_COMMAND_REGEX.exec(text) - names.push match[1] + while match = re.exec(text) + result.push {name: match[1], whitespace: match[2]} iterations += 1 if iterations >= 1000 - return names - return names + return result + return result class SnippetManager getCompletions: (editor, session, pos, prefix, callback) -> - # console.log ">> get snippet completions", editor, session, pos, prefix docText = session.getValue() - customEnvironmentNames = parseCustomEnvironmentNames(docText) - beginCommandNames = parseBeginCommandNames(docText) - # console.log customEnvironmentNames - parsedNames = _.union(customEnvironmentNames, beginCommandNames) + customEnvironments = parseCustomEnvironments(docText) + beginCommands = parseBeginCommands(docText) + parsedItems = _.union(customEnvironments, beginCommands) snippets = staticSnippets.concat( - parsedNames.map (name) -> + parsedItems.map (item) -> { - caption: "\\begin{#{name}}..." + caption: "\\begin{#{item.name}}..." snippet: """ - \\begin{#{name}} - $1 - \\end{#{name}} + \\begin{#{item.name}} + #{item.whitespace || ''}$1 + \\end{#{item.name}} """ meta: "env" } @@ -144,10 +142,10 @@ define () -> # 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 # *shrug* - parsedNames.map (name) -> + parsedItems.map (item) -> { - caption: "\\end{#{name}}" - value: "\\end{#{name}}" + caption: "\\end{#{item.name}}" + value: "\\end{#{item.name}}" meta: "env" } )