Skip to content

Commit 4e4df47

Browse files
committed
add small features
go-to-declaration support column catch crag command stderr use -R instead of (fs-plus).traverseTreeSync better log search tag limit max
1 parent 26eb10b commit 4e4df47

File tree

5 files changed

+80
-83
lines changed

5 files changed

+80
-83
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ autocomplete with ctags dependent on [autocomplete-plus](https://github.com/sasc
3131
* Writing Tests
3232
* Auto check package of autocomplete-plus installed
3333
* ~~Auto disable package of symbols-view~~
34+
* use Activation Events to speed up load time
35+
* ~~use ctags command args -R~~
36+
37+
38+
#Changelog
39+
*go-to-declaration support column

lib/ctags-cache.coffee

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,66 @@
11

22
TagGenerator = require './tag-generator'
33
fs = require 'fs-plus'
4-
minimatch = require "minimatch"
54

6-
{Point} = require "atom"
5+
matchOpt = {matchBase: true}
76

87
module.exports =
9-
activate: (build) ->
8+
activate: () ->
109
@cachedTags = {}
1110

12-
if build
13-
@rebuild()
14-
1511
deactivate: ->
1612
@cachedTags = null
1713

18-
#options = { partialMatch: true }
14+
#options = { partialMatch: true, maxItems }
1915
findTags: (prefix, options) ->
2016
tags = []
17+
empty = true
2118
for key, value of @cachedTags
22-
tags.push (value.filter (x) ->
23-
if options and options.partialMatch == true
24-
return x.name.indexOf(prefix) == 0
25-
else
26-
return x.name == prefix
27-
)...
19+
empty = false
20+
for tag in value
21+
if options?.partialMatch and tag.name.indexOf(prefix) == 0
22+
tags.push tag
23+
else if tag.name == prefix
24+
tags.push tag
25+
return tags if options?.maxItems and tags.length == options.maxItems
26+
27+
#TODO: prompt in editor
28+
console.warn("[atom-ctags:findTags] tags empty, did you RebuildTags?") if empty
2829
return tags
2930

30-
# Private: Checks whether the file is blacklisted
31-
#
32-
# Returns {Boolean} that defines whether the file is blacklisted
33-
FileBlacklisted: (blacklist, f, opt) ->
34-
for blacklistGlob in blacklist
35-
if minimatch(f, blacklistGlob, opt)
36-
return true
37-
return false
38-
39-
listTreeSync: (rootPath) ->
40-
blacklist = (atom.config.get("atom-ctags.fileBlacklist") or "")
41-
.split ","
42-
.map (s) -> s.trim()
43-
44-
opt = {matchBase: true}
45-
paths = []
46-
47-
onPath = (filePath) =>
48-
if @FileBlacklisted(blacklist, filePath, opt)
49-
return false
50-
paths.push(filePath)
51-
return true
52-
53-
onDirectory = (dirPath) =>
54-
return not @FileBlacklisted(blacklist, dirPath, opt)
55-
56-
fs.traverseTreeSync(rootPath, onPath, onDirectory)
57-
return paths
58-
59-
rebuild: ->
60-
61-
list = @listTreeSync(atom.project.getPath())
62-
@generateTags(f) for f in list
63-
64-
getScopeName: -> atom.workspace.getActiveEditor()?.getGrammar()?.scopeName
65-
6631
getTagLine: (tag) ->
67-
return unless tag.pattern
6832
file = atom.project.resolve(tag.file)
69-
return unless fs.isFileSync(file)
33+
if not fs.isFileSync(file)
34+
console.error "[atom-ctags:getTagLine] @#{tag.file}@ not exist?"
35+
return
36+
7037
debug = []
7138
for line, index in fs.readFileSync(file, 'utf8').split('\n')
7239
if line.indexOf(tag.pattern) == 0
73-
return new Point(index, 0)
40+
tag.position.row = index
41+
return true
42+
43+
console.error "[atom-ctags:getTagLine] @#{tag.pattern}@ not find in @#{tag.file}@?"
44+
return true
7445

75-
generateTags:(filePath, callback) ->
76-
new TagGenerator(filePath, @getScopeName()).generate().done (matches) =>
77-
tags = []
78-
for match in matches
79-
match.position = @getTagLine(match)
80-
if match.position
81-
tags.push(match)
46+
generateTags:(path, callback) ->
47+
delete @cachedTags[path]
8248

83-
@cachedTags[filePath] = tags
84-
if callback
85-
callback(tags)
49+
scopeName = atom.workspace.getActiveEditor()?.getGrammar()?.scopeName
50+
new TagGenerator(path, scopeName).generate().done (tags) =>
51+
ret = [] if callback
52+
for tag in tags
53+
if @getTagLine(tag)
54+
ret.push tag if callback
55+
data = @cachedTags[tag.file]
56+
if not data
57+
data = []
58+
@cachedTags[tag.file] = data
59+
data.push tag
60+
61+
callback?(ret)
8662

8763
getOrCreateTags: (filePath, callback) ->
8864
tags = @cachedTags[filePath]
89-
if tags
90-
if callback
91-
callback(tags)
92-
return
93-
generateTags(filePath, callback)
65+
return callback?(tags) if tags
66+
@generateTags(filePath, callback)

lib/ctags-provider.coffee

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
module.exports =
33
ProviderClass: (Provider, Suggestion, ctagsCache) ->
4-
options = { partialMatch: true }
4+
#maxItems = autocomplete-plus:SimpleSelectListView.maxItems
5+
options = { partialMatch: true, maxItems: 10 }
56
basepath = atom.project.getPath()+"/"
7+
68
class CtagsProvider extends Provider
79
buildSuggestions: ->
8-
console.error("buildSuggestions")
910

1011
selection = @editor.getSelection()
1112
prefix = @prefixOfSelection selection

lib/symbols-view.coffee

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
path = require 'path'
21
{$$, SelectListView} = require 'atom'
32
fs = require 'fs-plus'
43

54
module.exports =
65
class SymbolsView extends SelectListView
7-
@activate: ->
8-
new SymbolsView
96

107
initialize: (@stack) ->
118
super
@@ -15,8 +12,6 @@ class SymbolsView extends SelectListView
1512
@cancel()
1613
@remove()
1714

18-
getFilterKey: -> 'name'
19-
2015
viewForItem: ({position, name, file}) ->
2116
$$ ->
2217
@li class: 'two-lines', =>
@@ -55,12 +50,11 @@ class SymbolsView extends SelectListView
5550

5651
@stack.push(previous)
5752

58-
moveToPosition: (position, beginningOfLine=true) ->
53+
moveToPosition: (position) ->
5954
editorView = atom.workspaceView.getActiveView()
6055
if editor = editorView.getEditor?()
6156
editorView.scrollToBufferPosition(position, center: true)
6257
editor.setCursorBufferPosition(position)
63-
editor.moveCursorToFirstCharacterOfLine() if beginningOfLine
6458

6559
attach: ->
6660
@storeFocusedElement()

lib/tag-generator.coffee

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{BufferedProcess, Point} = require 'atom'
22
Q = require 'q'
33
path = require 'path'
4+
{Point} = require "atom"
45

56
module.exports =
67
class TagGenerator
@@ -21,9 +22,9 @@ class TagGenerator
2122
tag.pattern = pattern.match(/^\/\^(.*)(\/;")/)?[1]
2223

2324
if tag.pattern
24-
tag.pattern = tag.pattern.replace(/\\\\/g, "\\")
25-
tag.pattern = tag.pattern.replace(/\\\//g, "/")
26-
25+
tag.position = new Point(0, tag.pattern.indexOf(tag.name))
26+
else
27+
return null
2728
return tag
2829
else
2930
return null
@@ -64,15 +65,37 @@ class TagGenerator
6465
if language = @getLanguage()
6566
args.push("--language-force=#{language}")
6667

67-
args.push('-f', '-', @path)
68+
args.push('-R', '-f', '-', @path)
6869

6970
stdout = (lines) =>
70-
for line in lines.split('\n')
71+
lines = lines.replace(/\\\\/g, "\\")
72+
lines = lines.replace(/\\\//g, "/")
73+
74+
lines = lines.split('\n')
75+
if lines[lines.length-1] == ""
76+
lines.pop()
77+
78+
for line in lines
7179
tag = @parseTagLine(line)
72-
tags.push(tag) if tag
80+
if tag
81+
tags.push(tag)
82+
else
83+
console.error """
84+
[atom-ctags:TagGenerator] please create a new issue:
85+
failed to parseTagLine, @#{line}@
86+
command: @#{command} #{args.join(' ')}@
87+
"""
88+
stderr = (lines) =>
89+
alert """
90+
[atom-ctags:TagGenerator]
91+
please create a new issue:
92+
failed to excute command: @#{command} #{args.join(' ')}@
93+
lines: @#{lines}@
94+
"""
95+
7396
exit = ->
7497
deferred.resolve(tags)
7598

76-
new BufferedProcess({command, args, stdout, exit})
99+
new BufferedProcess({command, args, stdout, stderr, exit})
77100

78101
deferred.promise

0 commit comments

Comments
 (0)