Commits

abudden committed e591d4b

Improvements to cscope interface so it only runs with C code by default.

Comments (0)

Files changed (5)

autoload/TagHighlight/Generation.vim

 		endif
 	endfor
 
+	if TagHighlight#Option#GetOption('EnableCscope')
+		call TagHighlight#Cscope#PauseCscope()
+		if ! has_key(b:TagHighlightPrivate, 'CscopeFileInfo')
+			let b:TagHighlightPrivate['CscopeFileInfo'] = TagHighlight#Find#LocateFile('CSCOPE', '')
+		endif
+		if b:TagHighlightPrivate['CscopeFileInfo']['Found'] == 1
+			let b:TagHighlightSettings['CscopeFileLocation'] = b:TagHighlightPrivate['CscopeFileInfo']['Directory']
+		endif
+	endif
+
 	let RunOptions = TagHighlight#Option#CopyOptions()
 	if TagHighlight#Option#GetOption('EnableCscope')
-		call TagHighlight#Cscope#PauseCscope()
 		if TagHighlight#Option#GetOption('CscopeOnlyIfPresent')
-			if ! has_key(b:TagHighlightPrivate, 'CscopeFileInfo')
-				let b:TagHighlightPrivate['CscopeFileInfo'] = TagHighlight#Find#LocateFile('CSCOPE', '')
-			endif
 			if ! b:TagHighlightPrivate['CscopeFileInfo']['Exists']
 				let RunOptions['EnableCscope'] = 1
 			endif
 		endif
 	endif
 
+	call TagHLDebug("Running generator with options:", "Information")
+	for var in ["g:TagHighlightSettings","b:TagHighlightConfigFileOptions","b:TagHighlightSettings"]
+		if exists(var)
+			call TagHLDebug(" - " . var . ": " . string(eval(var)), "Information")
+		else
+			call TagHLDebug(" - " . var . ": UNSET", "Information")
+		endif
+	endfor
+
+
 	call TagHighlight#RunPythonScript#RunGenerator(RunOptions)
 
 	if TagHighlight#Option#GetOption('EnableCscope')

doc/TagHighlight.txt

 		CscopeOnlyIfPresent              Link:|TagHL-CscopeOnlyIfPresent|
 			Only generate a new cscope database if one exists already.
 
+		CscopeOnlyIfCCode                Link:|TagHL-CscopeOnlyIfCCode|
+			Only generate a new cscope database if C/C++ code detected.
+
 	Locations of Files (normal):
 
 		DefaultDirModeSearchWildcards    Link:|TagHL-DirModeSearchWildcards|
 		CtagsFileLocation                Link:|TagHL-CtagsFileLocation|
 			Internal use only.
 
+		CscopeFileLocation               Link:|TagHL-CscopeFileLocation|
+			Internal use only.
+
 		TypesFileLocation                Link:|TagHL-TypesFileLocation|
 			Internal use only.
 
 		Option Type: String
 		Default: None (no explicit directory)
 
+	CscopeFileLocation                   *TagHL-CscopeFileLocation*
+		Used internally to handle the location of the cscope file.  Do not use
+		this option as it will be automatically overwritten.  Instead, see
+		|TagHL-DefaultDirModePriority|, |TagHL-TagFileDirModePriority| and
+		|TagHL-TagFileDirectory|.
+
+		See also |TagHL-CscopeFileName|.
+
+
 	CscopeFileName                       *TagHL-CscopeFileName*
 		By default, the file generated by cscope containing the database is
 		named "cscope.out".  If you wish to change this, adjust this option
 
 		Option Type: String
 		Default: "cscope"
+	
+	CscopeOnlyIfCCode                    *TagHL-CscopeOnlyIfCCode*
+		If the cscope interface is enabled (see |TagHL-EnableCscope|) and this
+		option is set to True or 1 (the default), a new cscope database will
+		only be generated if C or C++ code has been detected during the tag
+		generation OR if cscope.out already exists.  This means that if
+		cscope.out (or whatever you've set |TagHL-CscopeFileName| to) doesn't
+		exist and you have C code, the FIRST run of |UpdateTypesFile| will
+		take slightly longer (as the cscope database will be created after
+		running ctags rather than in parallel).  If a cscope database exists,
+		a new one will be created regardless.  If you want the cscope database
+		to be created unconditionally, set this option to False or 0.
+
+		Option Type: Boolean
+		Default: True
 
 	CscopeOnlyIfPresent                  *TagHL-CscopeOnlyIfPresent*
-		If the cscope interface is enabled (see |TagHL-EnableCscope| and this
+		If the cscope interface is enabled (see |TagHL-EnableCscope|) and this
 		option is set to True or 1, a new cscope database will only be
 		generated if one exists already.  Otherwise, whenever tags and types
 		are generated, a cscope database will be generated as well.
 	To speed up the process, cscope and ctags are run in parallel in separate
 	threads.
 
-	In practice, the only options likely to be needed are |TagHL-EnableCscope|
-	and |TagHL-CscopeOnlyIfPresent|.  The latter option allows the cscope
-	interface to be turned on permanently and cscope databases will only be
-	generated automatically after the first one has been generated manually by
-	navigating to the project root and running "cscope -b".  This is mainly
-	useful if you work on C projects and on projects in other languages (for
-	which cscope probably isn't really useful so generating a database is
-	pointless).
+	In practice, the only options likely to be needed are |TagHL-EnableCscope|,
+	|TagHL-CscopeOnlyIfCCode| and |TagHL-CscopeOnlyIfPresent|.  Most users
+	should be able to set |TagHL-EnableCscope| to 'True' and everything will
+	just work: databases will be created for C/C++ code but for nothing else.
+	If you don't like the fact that the first ever run of |UpdateTypesFile| on
+	your C code takes slightly longer, look at |TagHL-CscopeOnlyIfCCode|.  The
+	other option (|TagHL-CscopeOnlyIfPresent|) allows the cscope interface to
+	be turned on permanently and cscope databases will only be generated
+	automatically after the first one has been generated manually by
+	navigating to the project root and running "cscope -b".  This gives a bit
+	more control if required.
 
 	In addition to the |TagHighlight| options, it is important to ensure that
 	Vim's 'cscopeprg' is set appropriately.

plugin/TagHighlight/data/options.txt

 	Default:False
 	Help:Enable cscope support in TagHighlight
 
+only_generate_cscope_db_for_c_code:
+	VimOptionMap:CscopeOnlyIfCCode
+	Type:bool
+	Default:True
+	Help:Only generate cscope database if one exists or if code is C code.
+
 cscope_file_name:
 	CommandLineSwitches:--cscope-filename
 	VimOptionMap:CscopeFileName
 	Default:.
 	Help:Location in which to store or find CTAGS output file
 
+cscope_file_dir:
+	CommandLineSwitches:--cscope-file-dir
+	VimOptionMap:CscopeFileLocation
+	Type:string
+	Default:.
+	Help:Location in which to store or find cscope output file
+
 types_file_name_override:
 	CommandLineSwitches:--types-file-name-override
 	VimOptionMap:TypesFileNameForce

plugin/TagHighlight/module/cscope_interface.py

 import os
 import threading
 
+from .debug import Debug
+
 class CscopeThread(threading.Thread):
     def __init__(self, root, command):
         self.root = root
     global cscopeThread
     root = options['source_root']
 
-    args = ['-b', '-f', options['cscope_file_name']]
+    args = ['-b', '-f', options['cscope_file_full']]
 
     if options['recurse']:
         args.append('-R')
 
     cscope_cmd = [options['cscope_exe_full']] + args
 
-    #Debug("cscope command is " + repr(cscope_cmd), "Information")
+    Debug("cscope command is " + repr(cscope_cmd), "Information")
 
     cscopeThread = CscopeThread(root, cscope_cmd)
     cscopeThread.start()

plugin/TagHighlight/module/worker.py

     from .ctags_interface import GenerateTags, ParseTags
     from .generation import CreateTypesFile
 
+    cscope_check_c = False
     if config['enable_cscope']:
-        from .cscope_interface import StartCscopeDBGeneration, CompleteCscopeDBGeneration
-        StartCscopeDBGeneration(config)
+        cscope_file = os.path.join(config['cscope_file_dir'], config['cscope_file_name'])
+        config['cscope_file_full'] = cscope_file
+        if os.path.exists(cscope_file) or not config['only_generate_cscope_db_for_c_code']:
+            Debug("Running cscope", "Information")
+            from .cscope_interface import StartCscopeDBGeneration, CompleteCscopeDBGeneration
+            StartCscopeDBGeneration(config)
+        else:
+            Debug("Deferring cscope until C code detected", "Information")
+            cscope_check_c = True
 
     if not config['use_existing_tagfile']:
         Debug("Generating tag file", "Information")
             CreateTypesFile(config, language, tag_db[language], file_tag_db[language])
 
     if config['enable_cscope']:
+        if cscope_check_c and 'c' in tag_db:
+            Debug("Running cscope as C code detected", "Information")
+            from .cscope_interface import StartCscopeDBGeneration, CompleteCscopeDBGeneration
+            StartCscopeDBGeneration(config)
         CompleteCscopeDBGeneration()
 
     os.chdir(start_directory)