Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Add limit option to findTags.
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonw committed Feb 23, 2016
1 parent 50a8be0 commit a166484
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Get all tags matching the tag specified from the tags file at the path.
(default: `false`)
* `partialMatch` - `true` to include tags that partially match the given tag
(default: `false`)
* `limit` - maximum number of matches to return. Should be a positive integer.
(default: unlimited)

* `callback` - The function to call when complete with an error as the first
argument and an array containing objects that have `name` and
Expand Down
19 changes: 19 additions & 0 deletions spec/ctags-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ describe 'ctags', ->
expect(tags[0].kind).toBe 'f'
expect(tags[0].lineNumber).toBe 0

describe 'when limit is set', ->
it 'returns the correct number of tags', ->
callback = jasmine.createSpy('callback')
tags = ctags.findTags(tagsFile, 'dup', {partialMatch: true, limit: 1}, callback)

waitsFor ->
callback.callCount is 1

runs ->
expect(callback.argsForCall[0][0]).toBeFalsy()
tags = callback.argsForCall[0][1]

expect(tags.length).toBe 1
expect(tags[0].name).toBe 'duplicate'

it 'throws for invalid values', ->
expect(() -> ctags.findTags(tagsFile, '', limit: 'test')).toThrow()
expect(() -> ctags.findTags(tagsFile, '', limit: 0)).toThrow()

describe '.createReadStream(tagsFilePath)', ->
it 'returns a stream that emits data and end events', ->
stream = ctags.createReadStream(tagsFile)
Expand Down
6 changes: 4 additions & 2 deletions src/ctags.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ exports.findTags = (tagsFilePath, tag, options, callback) ->
callback = options
options = null

{partialMatch, caseInsensitive} = options ? {}
{partialMatch, caseInsensitive, limit} = options ? {}
if limit? and (not Number.isInteger(limit) or limit <= 0)
throw new TypeError('limit must be a positive integer')

tagsWrapper = new Tags(tagsFilePath)
tagsWrapper.findTags tag, partialMatch, caseInsensitive, (error, tags) ->
tagsWrapper.findTags tag, partialMatch, caseInsensitive, limit, (error, tags) ->
tagsWrapper.end()
callback?(error, tags)

Expand Down
3 changes: 2 additions & 1 deletion src/tag-finder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ void TagFinder::Execute() {
tagEntry entry;
if (tagsFind(file, &entry, tag.data(), options) == TagSuccess) {
matches.push_back(Tag(entry));
while (tagsFindNext(file, &entry) == TagSuccess)
while ((limit == 0 || matches.size() < limit) &&
tagsFindNext(file, &entry) == TagSuccess)
matches.push_back(Tag(entry));
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/tag-finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ using namespace v8;

class TagFinder : public Nan::AsyncWorker {
public:
TagFinder(Nan::Callback *callback, std::string tag, int options, tagFile *file)
: Nan::AsyncWorker(callback), options(options), tag(tag), file(file) {}
TagFinder(Nan::Callback* callback,
std::string tag,
int options,
size_t limit,
tagFile* file)
: Nan::AsyncWorker(callback),
options(options),
limit(limit),
tag(tag),
file(file) {}

~TagFinder() {}

Expand All @@ -21,6 +29,7 @@ class TagFinder : public Nan::AsyncWorker {

private:
int options;
size_t limit;
std::string tag;
std::vector< Tag > matches;
tagFile *file;
Expand Down
5 changes: 3 additions & 2 deletions src/tags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ NAN_METHOD(Tags::FindTags) {
else
options |= TAG_OBSERVECASE;

Nan::Callback *callback = new Nan::Callback(info[3].As<Function>());
Nan::AsyncQueueWorker(new TagFinder(callback, tag, options, tagFile));
size_t limit = info[3]->IntegerValue();
Nan::Callback *callback = new Nan::Callback(info[4].As<Function>());
Nan::AsyncQueueWorker(new TagFinder(callback, tag, options, limit, tagFile));
info.GetReturnValue().SetUndefined();
}

Expand Down

0 comments on commit a166484

Please sign in to comment.