From 7ed69353a9a75cf0c5a6bdf1654c759a04129524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Walle?= Date: Wed, 8 Nov 2017 10:40:32 +0100 Subject: [PATCH 1/2] Cache the version information This function will be called more than once later. Cache the major version for efficiency. Instead of parsing the cmake output with regexes, assume the first line of version output and parse out the major version immediately. --- autoload/cmakecomplete.vim | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/autoload/cmakecomplete.vim b/autoload/cmakecomplete.vim index 320957a..1cfe3d5 100644 --- a/autoload/cmakecomplete.vim +++ b/autoload/cmakecomplete.vim @@ -34,6 +34,7 @@ let s:cmake_properties = [] let s:cmake_modules = [] let s:cmake_variables = [] let s:cmake_command_examples = {} +let s:cmake_major_version = 0 function s:createbuffer() let counter = 0 @@ -97,13 +98,14 @@ function cmakecomplete#PrintExamples() endfunc function cmakecomplete#Version() - let output = system('cmake --version') - for c in split(output, '\n') - if c =~ 'version' - let components = split(c, ' ') - return components[len(components) - 1] - endif - endfor + if s:cmake_major_version > 0 + return s:cmake_major_version + else + let output = systemlist('cmake --version') + let version_number = split(output[0], ' ')[-1] + let s:cmake_major_version = split(version_number, '\.')[0] + return s:cmake_major_version + endif endfunc function cmakecomplete#Init3(help, list, ignore_case) @@ -355,7 +357,7 @@ function cmakecomplete#HelpComplete(ArgLead, CmdLine, CursorPos) return result endfunction -if cmakecomplete#Version() =~ "^2\." +if cmakecomplete#Version() == 2 call cmakecomplete#Init('commands', s:cmake_commands, 1) call cmakecomplete#Init('properties', s:cmake_properties, 0) call cmakecomplete#Init('modules', s:cmake_modules, 1) From 4e9307fd0429f24b3a87586c96168909c350b4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Walle?= Date: Wed, 8 Nov 2017 15:02:35 +0100 Subject: [PATCH 2/2] Fill list of completion data on demand Instead of initalizing all the data as soon as anything is requested, only initialize the lists that are actually needed. Do this by introducing a "getter" for the lists instead of referencing s:cmake_foo directly. The getter checks if the list is empty and then fills it. The many more calls to Version() is mitigated by the previous commit. Currently this achieves nothing of value for the Help() function, but does no harm either. --- autoload/cmakecomplete.vim | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/autoload/cmakecomplete.vim b/autoload/cmakecomplete.vim index 1cfe3d5..1ed7061 100644 --- a/autoload/cmakecomplete.vim +++ b/autoload/cmakecomplete.vim @@ -46,12 +46,27 @@ function s:createbuffer() return versionedName endfunction +function! s:getCompletionList(listname) + if index(['commands', 'properties', 'variables', 'modules'], a:listname) == -1 + return [] + endif + let completion_list = get(s:, 'cmake_' . a:listname, []) + if empty(completion_list) + if cmakecomplete#Version() == 2 + call cmakecomplete#Init(a:listname, completion_list, a:listname == 'properties' ? 0 : 1) + else + call cmakecomplete#Init3(a:listname, completion_list, a:listname == 'properties' ? 0 : 1) + endif + endif + return completion_list +endfunction + function cmakecomplete#Help(...) " create a new buffer and show all of cmake's help there let output = "" if a:0 == 1 let arg = tolower(a:1) - let searchlist = [s:cmake_commands, s:cmake_properties, s:cmake_modules, s:cmake_variables] + let searchlist = [s:getCompletionList('commands'), s:getCompletionList('properties'), s:getCompletionList('modules'), s:getCompletionList('variables')] for sl in searchlist for m in sl if m['word'] == arg @@ -312,15 +327,15 @@ function! cmakecomplete#Complete(findstart, base) endif let res = [] - let list = s:cmake_commands + let list = s:getCompletionList('commands') let match = '^' . tolower(a:base) if cmakecomplete#InVariable() let match = '^' . a:base - let list = s:cmake_variables + let list = s:getCompletionList('variables') elseif cmakecomplete#InInclude() " return modules let match = '^' . a:base - let list = s:cmake_modules + let list = s:getCompletionList('modules') elseif cmakecomplete#InFunction() " return completion variables let match = '^' . a:base @@ -328,11 +343,11 @@ function! cmakecomplete#Complete(findstart, base) if has_key(s:cmake_command_examples, fname) let list = cmakecomplete#GetArguments(fname) else - let list = s:cmake_properties + let list = s:getCompletionList('properties') endif let prevword = cmakecomplete#PreviousWord() if prevword == "PROPERTIES" - let list = s:cmake_properties + let list = s:getCompletionList('properties') endif endif " return the completion words @@ -348,7 +363,7 @@ endfunction function cmakecomplete#HelpComplete(ArgLead, CmdLine, CursorPos) let result = [] let match = '^' . a:ArgLead - for m in s:cmake_commands + for m in s:getCompletionList('commands') let w = m['word'] if w =~ match call add(result, w) @@ -357,17 +372,5 @@ function cmakecomplete#HelpComplete(ArgLead, CmdLine, CursorPos) return result endfunction -if cmakecomplete#Version() == 2 -call cmakecomplete#Init('commands', s:cmake_commands, 1) -call cmakecomplete#Init('properties', s:cmake_properties, 0) -call cmakecomplete#Init('modules', s:cmake_modules, 1) -call cmakecomplete#Init('variables', s:cmake_variables, 1) -else -call cmakecomplete#Init3('commands', s:cmake_commands, 1) -call cmakecomplete#Init3('properties', s:cmake_properties, 0) -call cmakecomplete#Init3('modules', s:cmake_modules, 1) -call cmakecomplete#Init3('variables', s:cmake_variables, 1) -endif - let &cpo = s:keepcpo unlet s:keepcpo