?? namespaces.vim
字號(hào):
" Description: Omni completion script for cpp files" Maintainer: Vissale NEANG" Last Change: 25 jun 2006let g:omni#cpp#namespaces#CacheResolve = {}let g:omni#cpp#namespaces#CacheUsing = {}" TODO: For the next release"let g:omni#cpp#namespaces#CacheAlias = {}" Get the using namespace list from a linefunction! s:GetNamespaceAliasListFromLine(szLine) let result = {} let tokens = omni#cpp#tokenizer#Tokenize(a:szLine) let szAlias = '' let szNamespace = '' let state = 0 for token in tokens if state==0 let szAlias = '' let szNamespace = '' if token.value == '/*' let state = 1 elseif token.value == '//' " It's a comment let state = -1 break elseif token.value == 'namespace' let state = 2 endif elseif state==1 if token.value == '*/' let state=0 endif elseif state==2 if token.kind == 'cppWord' let szAlias .= token.value let state = 3 else let state = -1 break endif elseif state == 3 if token.value == '=' let state = 4 else let state = -1 break endif elseif state == 4 if token.value == '::' let szNamespace .= token.value let state = 5 elseif token.kind == 'cppWord' let szNamespace .= token.value let state = 6 " Maybe end of tokens endif elseif state==5 if token.kind == 'cppWord' let szNamespace .= token.value let state = 6 " Maybe end of tokens else " Error, we can't have 'namespace ALIAS = Something::' let state = -1 break endif elseif state==6 if token.value == '::' let szNamespace .= token.value let state = 5 else call extend(result, {szAlias : szNamespace}) let state = 0 endif endif endfor if state == 6 call extend(result, {szAlias : szNamespace}) endif return resultendfunc" Get the using namespace list from a linefunction! s:GetNamespaceListFromLine(szLine) let result = [] let tokens = omni#cpp#tokenizer#Tokenize(a:szLine) let szNamespace = '' let state = 0 for token in tokens if state==0 let szNamespace = '' if token.value == '/*' let state = 1 elseif token.value == '//' " It's a comment let state = -1 break elseif token.value == 'using' let state = 2 endif elseif state==1 if token.value == '*/' let state=0 endif elseif state==2 if token.value == 'namespace' let state = 3 else " Error, 'using' must be followed by 'namespace' let state = -1 break endif elseif state==3 if token.value == '::' let szNamespace .= token.value let state = 4 elseif token.kind == 'cppWord' let szNamespace .= token.value let state = 5 " Maybe end of tokens endif elseif state==4 if token.kind == 'cppWord' let szNamespace .= token.value let state = 5 " Maybe end of tokens else " Error, we can't have 'using namespace Something::' let state = -1 break endif elseif state==5 if token.value == '::' let szNamespace .= token.value let state = 4 else call extend(result, [szNamespace]) let state = 0 endif endif endfor if state == 5 call extend(result, [szNamespace]) endif return resultendfunc" Get the namespace list from a namespace mapfunction! s:GetUsingNamespaceListFromMap(namespaceMap, ...) let stopLine = 0 if a:0>0 let stopLine = a:1 endif let result = [] let keys = sort(keys(a:namespaceMap), 'omni#common#utils#CompareNumber') for i in keys if stopLine != 0 && i > stopLine break endif call extend(result, a:namespaceMap[i]) endfor return resultendfunc" Get global using namespace list from the current bufferfunction! omni#cpp#namespaces#GetListFromCurrentBuffer(...) let namespaceMap = s:GetAllUsingNamespaceMapFromCurrentBuffer() let result = [] if namespaceMap != {} let result = s:GetUsingNamespaceListFromMap(namespaceMap, (a:0 > 0)? a:1 : line('.')) endif return resultendfunc" Get global using namespace map from the current buffer and include files recursivelyfunction! s:GetAllUsingNamespaceMapFromCurrentBuffer(...) let includeGuard = (a:0>0)? a:1 : {} let szFilePath = omni#cpp#utils#ResolveFilePath(getreg('%')) let namespaceMap = {} if has_key(includeGuard, szFilePath) return namespaceMap else let includeGuard[szFilePath] = 1 endif let namespaceMap = omni#cpp#namespaces#GetMapFromCurrentBuffer() if g:OmniCpp_NamespaceSearch != 2 " We don't search included files if OmniCpp_NamespaceSearch != 2 return namespaceMap endif for inc in omni#cpp#includes#GetList() let lnum = inc.pos[0] let tmpMap = s:GetAllUsingNamespaceMapFromFile(inc.include, includeGuard) if tmpMap != {} if has_key(namespaceMap, lnum) call extend(namespaceMap[lnum], s:GetUsingNamespaceListFromMap(tmpMap)) else let namespaceMap[lnum] = s:GetUsingNamespaceListFromMap(tmpMap) endif endif endfor return namespaceMapendfunc" Get global using namespace map from a file and include files recursivelyfunction! s:GetAllUsingNamespaceMapFromFile(szFilePath, ...) let includeGuard = {} if a:0 >0 let includeGuard = a:1 endif let szFilePath = omni#cpp#utils#ResolveFilePath(a:szFilePath) let namespaceMap = {} if has_key(includeGuard, szFilePath) return namespaceMap else let includeGuard[szFilePath] = 1 endif " If g:OmniCpp_NamespaceSearch == 1 (search namespaces only in the current " buffer) we don't use cache for the current buffer let namespaceMap = omni#cpp#namespaces#GetMapFromBuffer(szFilePath, g:OmniCpp_NamespaceSearch==1) if g:OmniCpp_NamespaceSearch != 2 " We don't search included files if OmniCpp_NamespaceSearch != 2 return namespaceMap endif for inc in omni#cpp#includes#GetList(szFilePath) let lnum = inc.pos[0] let tmpMap = s:GetAllUsingNamespaceMapFromFile(inc.include, includeGuard) if tmpMap != {} if has_key(namespaceMap, lnum) call extend(namespaceMap[lnum], s:GetUsingNamespaceListFromMap(tmpMap)) else let namespaceMap[lnum] = s:GetUsingNamespaceListFromMap(tmpMap) endif endif endfor return namespaceMapendfunc" Get global using namespace map from a the current bufferfunction! omni#cpp#namespaces#GetMapFromCurrentBuffer() let namespaceMap = {} let originalPos = getpos('.') call setpos('.', [0, 1, 1, 0]) let curPos = [1,1] while curPos != [0,0] let curPos = searchpos('\C^using\s\+namespace', 'W') if curPos != [0,0] let szLine = getline('.') let startPos = curPos[1] let endPos = match(szLine, ';', startPos-1) if endPos!=-1 " We get the namespace list from the line let namespaceMap[curPos[0]] = s:GetNamespaceListFromLine(szLine) endif endif endwhile call setpos('.', originalPos) return namespaceMapendfunc" Get global using namespace map from a filefunction! omni#cpp#namespaces#GetMapFromBuffer(szFilePath, ...) let bUpdate = 0 if a:0 > 0 let bUpdate = a:1 endif let szFilePath = omni#cpp#utils#ResolveFilePath(a:szFilePath) if !bUpdate && has_key(g:omni#cpp#namespaces#CacheUsing, szFilePath) return copy(g:omni#cpp#namespaces#CacheUsing[szFilePath]) endif let namespaceMap = {} " The file exists, we get the global namespaces in this file let szFixedPath = escape(szFilePath, g:omni#cpp#utils#szEscapedCharacters) execute 'silent! lvimgrep /\C^using\s\+namespace/gj '.szFixedPath " key = line number " value = list of namespaces let listQuickFix = getloclist(0) for qf in listQuickFix let szLine = qf.text let startPos = qf.col let endPos = match(szLine, ';', startPos-1) if endPos!=-1 " We get the namespace list from the line let namespaceMap[qf.lnum] = s:GetNamespaceListFromLine(szLine) endif endfor if szFixedPath != '' let g:omni#cpp#namespaces#CacheUsing[szFixedPath] = namespaceMap endif return copy(namespaceMap)endfunc" Get the stop position when searching for local variablesfunction! s:GetStopPositionForLocalSearch() " Stop position when searching a local variable let originalPos = getpos('.') let origPos = originalPos[1:2] let stopPosition = origPos let curPos = origPos while curPos !=[0,0] let stopPosition = curPos let curPos = searchpairpos('{', '', '}', 'bW', g:omni#cpp#utils#expIgnoreComments) endwhile call setpos('.', originalPos) return stopPositionendfunc" Get namespaces alias used at the cursor postion in a vim buffer" Note: The result depends on the current cursor position" @return" - Map of namespace aliasfunction! s:GetNamespaceAliasMap() " We store the cursor position because searchpairpos() moves the cursor let result = {} let originalPos = getpos('.') let origPos = originalPos[1:2] let stopPos = s:GetStopPositionForLocalSearch() let stopLine = stopPos[0] let curPos = origPos let lastLine = 0 let nextStopLine = origPos[0] let szReAlias = '\Cnamespace\s\+\w\+\s\+=' while curPos !=[0,0] let curPos = searchpos('}\|\('. szReAlias .'\)', 'bW',stopLine) if curPos!=[0,0] && curPos[0]!=lastLine let lastLine = curPos[0] let szLine = getline('.') if origPos[0] == curPos[0] " We get the line until cursor position let szLine = szLine[:origPos[1]] endif let szLine = omni#cpp#utils#GetCodeFromLine(szLine) if match(szLine, szReAlias)<0 " We found a '}' let curPos = searchpairpos('{', '', '}', 'bW', g:omni#cpp#utils#expIgnoreComments) else " We get the namespace alias from the line call extend(result, s:GetNamespaceAliasListFromLine(szLine)) let nextStopLine = curPos[0] endif endif endwhile " Setting the cursor to the original position call setpos('.', originalPos) call s:ResolveAliasKeys(result) return resultendfunc" Resolve an alias" eg: namespace IAmAnAlias1 = Ns1" eg: namespace IAmAnAlias2 = IAmAnAlias1::Ns2" => IAmAnAlias2 = Ns1::Ns2function! s:ResolveAliasKey(mapNamespaceAlias, szAlias) let szResult = a:mapNamespaceAlias[a:szAlias] " ::Ns1::Ns2::Ns3 => ['Ns1', 'Ns2', 'Ns3'] let listNamespace = split(szResult, '::') if len(listNamespace) " szBeginPart = 'Ns1' let szBeginPart = remove(listNamespace, 0) " Is 'Ns1' an alias ? if has_key(a:mapNamespaceAlias, szBeginPart) && szBeginPart != a:szAlias " Resolving alias 'Ns1' " eg: Ns1 = NsResolved let szResult = s:ResolveAliasKey(a:mapNamespaceAlias, szBeginPart) " szEndPart = 'Ns2::Ns3' let szEndPart = join(listNamespace, '::') if szEndPart != '' " Concatenation => szResult = 'NsResolved::Ns2::Ns3' let szResult .= '::' . szEndPart endif endif endif return szResultendfunc" Resolve all keys in the namespace alias mapfunction! s:ResolveAliasKeys(mapNamespaceAlias) let mapNamespaceAlias = a:mapNamespaceAlias call map(mapNamespaceAlias, 's:ResolveAliasKey(mapNamespaceAlias, v:key)')endfunc" Resolve namespace aliasfunction! omni#cpp#namespaces#ResolveAlias(mapNamespaceAlias, szNamespace) let szResult = a:szNamespace " ::Ns1::Ns2::Ns3 => ['Ns1', 'Ns2', 'Ns3'] let listNamespace = split(a:szNamespace, '::') if len(listNamespace) " szBeginPart = 'Ns1'
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -