Skip to content

Commit

Permalink
add small features
Browse files Browse the repository at this point in the history
go-to-declaration support column
catch crag command stderr
use -R instead of (fs-plus).traverseTreeSync
better log
search tag limit max
  • Loading branch information
yongkangchen committed Jul 5, 2014
1 parent 26eb10b commit 4e4df47
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 83 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ autocomplete with ctags dependent on [autocomplete-plus](https://github.com/sasc
* Writing Tests
* Auto check package of autocomplete-plus installed
* ~~Auto disable package of symbols-view~~
* use Activation Events to speed up load time
* ~~use ctags command args -R~~


#Changelog
*go-to-declaration support column
107 changes: 40 additions & 67 deletions lib/ctags-cache.coffee
Original file line number Diff line number Diff line change
@@ -1,93 +1,66 @@

TagGenerator = require './tag-generator'
fs = require 'fs-plus'
minimatch = require "minimatch"

{Point} = require "atom"
matchOpt = {matchBase: true}

module.exports =
activate: (build) ->
activate: () ->
@cachedTags = {}

if build
@rebuild()

deactivate: ->
@cachedTags = null

#options = { partialMatch: true }
#options = { partialMatch: true, maxItems }
findTags: (prefix, options) ->
tags = []
empty = true
for key, value of @cachedTags
tags.push (value.filter (x) ->
if options and options.partialMatch == true
return x.name.indexOf(prefix) == 0
else
return x.name == prefix
)...
empty = false
for tag in value
if options?.partialMatch and tag.name.indexOf(prefix) == 0
tags.push tag
else if tag.name == prefix
tags.push tag
return tags if options?.maxItems and tags.length == options.maxItems

#TODO: prompt in editor
console.warn("[atom-ctags:findTags] tags empty, did you RebuildTags?") if empty
return tags

# Private: Checks whether the file is blacklisted
#
# Returns {Boolean} that defines whether the file is blacklisted
FileBlacklisted: (blacklist, f, opt) ->
for blacklistGlob in blacklist
if minimatch(f, blacklistGlob, opt)
return true
return false

listTreeSync: (rootPath) ->
blacklist = (atom.config.get("atom-ctags.fileBlacklist") or "")
.split ","
.map (s) -> s.trim()

opt = {matchBase: true}
paths = []

onPath = (filePath) =>
if @FileBlacklisted(blacklist, filePath, opt)
return false
paths.push(filePath)
return true

onDirectory = (dirPath) =>
return not @FileBlacklisted(blacklist, dirPath, opt)

fs.traverseTreeSync(rootPath, onPath, onDirectory)
return paths

rebuild: ->

list = @listTreeSync(atom.project.getPath())
@generateTags(f) for f in list

getScopeName: -> atom.workspace.getActiveEditor()?.getGrammar()?.scopeName

getTagLine: (tag) ->
return unless tag.pattern
file = atom.project.resolve(tag.file)
return unless fs.isFileSync(file)
if not fs.isFileSync(file)
console.error "[atom-ctags:getTagLine] @#{tag.file}@ not exist?"
return

debug = []
for line, index in fs.readFileSync(file, 'utf8').split('\n')
if line.indexOf(tag.pattern) == 0
return new Point(index, 0)
tag.position.row = index
return true

console.error "[atom-ctags:getTagLine] @#{tag.pattern}@ not find in @#{tag.file}@?"
return true

generateTags:(filePath, callback) ->
new TagGenerator(filePath, @getScopeName()).generate().done (matches) =>
tags = []
for match in matches
match.position = @getTagLine(match)
if match.position
tags.push(match)
generateTags:(path, callback) ->
delete @cachedTags[path]

@cachedTags[filePath] = tags
if callback
callback(tags)
scopeName = atom.workspace.getActiveEditor()?.getGrammar()?.scopeName
new TagGenerator(path, scopeName).generate().done (tags) =>
ret = [] if callback
for tag in tags
if @getTagLine(tag)
ret.push tag if callback
data = @cachedTags[tag.file]
if not data
data = []
@cachedTags[tag.file] = data
data.push tag

callback?(ret)

getOrCreateTags: (filePath, callback) ->
tags = @cachedTags[filePath]
if tags
if callback
callback(tags)
return
generateTags(filePath, callback)
return callback?(tags) if tags
@generateTags(filePath, callback)
5 changes: 3 additions & 2 deletions lib/ctags-provider.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

module.exports =
ProviderClass: (Provider, Suggestion, ctagsCache) ->
options = { partialMatch: true }
#maxItems = autocomplete-plus:SimpleSelectListView.maxItems
options = { partialMatch: true, maxItems: 10 }
basepath = atom.project.getPath()+"/"

class CtagsProvider extends Provider
buildSuggestions: ->
console.error("buildSuggestions")

selection = @editor.getSelection()
prefix = @prefixOfSelection selection
Expand Down
8 changes: 1 addition & 7 deletions lib/symbols-view.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
path = require 'path'
{$$, SelectListView} = require 'atom'
fs = require 'fs-plus'

module.exports =
class SymbolsView extends SelectListView
@activate: ->
new SymbolsView

initialize: (@stack) ->
super
Expand All @@ -15,8 +12,6 @@ class SymbolsView extends SelectListView
@cancel()
@remove()

getFilterKey: -> 'name'

viewForItem: ({position, name, file}) ->
$$ ->
@li class: 'two-lines', =>
Expand Down Expand Up @@ -55,12 +50,11 @@ class SymbolsView extends SelectListView

@stack.push(previous)

moveToPosition: (position, beginningOfLine=true) ->
moveToPosition: (position) ->
editorView = atom.workspaceView.getActiveView()
if editor = editorView.getEditor?()
editorView.scrollToBufferPosition(position, center: true)
editor.setCursorBufferPosition(position)
editor.moveCursorToFirstCharacterOfLine() if beginningOfLine

attach: ->
@storeFocusedElement()
Expand Down
37 changes: 30 additions & 7 deletions lib/tag-generator.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{BufferedProcess, Point} = require 'atom'
Q = require 'q'
path = require 'path'
{Point} = require "atom"

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

if tag.pattern
tag.pattern = tag.pattern.replace(/\\\\/g, "\\")
tag.pattern = tag.pattern.replace(/\\\//g, "/")

tag.position = new Point(0, tag.pattern.indexOf(tag.name))
else
return null
return tag
else
return null
Expand Down Expand Up @@ -64,15 +65,37 @@ class TagGenerator
if language = @getLanguage()
args.push("--language-force=#{language}")

args.push('-f', '-', @path)
args.push('-R', '-f', '-', @path)

stdout = (lines) =>
for line in lines.split('\n')
lines = lines.replace(/\\\\/g, "\\")
lines = lines.replace(/\\\//g, "/")

lines = lines.split('\n')
if lines[lines.length-1] == ""
lines.pop()

for line in lines
tag = @parseTagLine(line)
tags.push(tag) if tag
if tag
tags.push(tag)
else
console.error """
[atom-ctags:TagGenerator] please create a new issue:
failed to parseTagLine, @#{line}@
command: @#{command} #{args.join(' ')}@
"""
stderr = (lines) =>
alert """
[atom-ctags:TagGenerator]
please create a new issue:
failed to excute command: @#{command} #{args.join(' ')}@
lines: @#{lines}@
"""

exit = ->
deferred.resolve(tags)

new BufferedProcess({command, args, stdout, exit})
new BufferedProcess({command, args, stdout, stderr, exit})

deferred.promise

0 comments on commit 4e4df47

Please sign in to comment.