This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathtag-reader.js
More file actions
133 lines (119 loc) · 4.78 KB
/
Copy pathtag-reader.js
File metadata and controls
133 lines (119 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/** @babel */
import { Task } from 'atom';
import ctags from 'ctags';
import async from 'async';
import getTagsFile from './get-tags-file';
import _ from 'underscore-plus';
let handlerPath = require.resolve('./load-tags-handler');
let wordAtCursor = (text, cursorIndex, wordSeparator, noStripBefore) => {
const beforeCursor = text.slice(0, cursorIndex);
const afterCursor = text.slice(cursorIndex);
const beforeCursorWordBegins = noStripBefore ? 0 : beforeCursor.lastIndexOf(wordSeparator) + 1;
let afterCursorWordEnds = afterCursor.indexOf(wordSeparator);
if (afterCursorWordEnds === -1) {
afterCursorWordEnds = afterCursor.length;
}
return beforeCursor.slice(beforeCursorWordBegins) + afterCursor.slice(0, afterCursorWordEnds);
};
export default {
find(editor, callback) {
let symbol;
const symbols = [];
if (symbol = editor.getSelectedText()) {
symbols.push(symbol);
}
if (!symbols.length) {
let nonWordCharacters;
const cursor = editor.getLastCursor();
const cursorPosition = cursor.getBufferPosition();
const scope = cursor.getScopeDescriptor();
const rubyScopes = scope.getScopesArray().filter(s => /^source\.ruby($|\.)/.test(s));
const puppetScopes = scope.getScopesArray().filter(s => /^source\.puppet($|\.)/.test(s));
const wordRegExp = rubyScopes.length ?
(nonWordCharacters = atom.config.get('editor.nonWordCharacters', {scope}),
// Allow special handling for fully-qualified ruby constants
nonWordCharacters = nonWordCharacters.replace(/:/g, ''),
new RegExp(`[^\\s${_.escapeRegExp(nonWordCharacters)}]+([!?]|\\s*=>?)?|[<=>]+`, 'g'))
:
cursor.wordRegExp();
const addSymbol = (symbol) => {
if (rubyScopes.length) {
// Normalize assignment syntax
if (/\s+=?$/.test(symbol)) { symbols.push(symbol.replace(/\s+=$/, '=')); }
// Strip away assignment & hashrocket syntax
symbols.push(symbol.replace(/\s+=>?$/, ''));
} else {
symbols.push(symbol);
}
};
// Can't use `getCurrentWordBufferRange` here because we want to select
// the last match of the potential 2 matches under cursor.
editor.scanInBufferRange(wordRegExp, cursor.getCurrentLineBufferRange(), ({range, match}) => {
if (range.containsPoint(cursorPosition)) {
symbol = match[0];
if (rubyScopes.length && symbol.indexOf(':') > -1) {
const cursorWithinSymbol = cursorPosition.column - range.start.column;
// Add fully-qualified ruby constant up until the cursor position
addSymbol(wordAtCursor(symbol, cursorWithinSymbol, ':', true));
// Additionally, also look up the bare word under cursor
addSymbol(wordAtCursor(symbol, cursorWithinSymbol, ':'));
} else if (puppetScopes.length && symbol.indexOf('::') > -1) {
const cursorWithinSymbol = cursorPosition.column - range.start.column;
// Add fully-qualified ruby constant up until the cursor position
addSymbol(wordAtCursor(symbol, cursorWithinSymbol, '::', true));
// Additionally, also look up the bare word under cursor
addSymbol(wordAtCursor(symbol, cursorWithinSymbol, '::'));
} else {
addSymbol(symbol);
}
}
});
}
if (!symbols.length) {
process.nextTick(() => {
callback(null, []);
});
}
async.map(atom.project.getPaths(), (projectPath, done) => {
const tagsFile = getTagsFile(projectPath);
let foundTags = [];
let foundErr = null;
const detectCallback = () => {
done(foundErr, foundTags);
};
if (!tagsFile) {
return detectCallback();
}
// Find the first symbol in the list that matches a tag
return async.detectSeries(symbols, (symbol, doneDetect) => {
ctags.findTags(tagsFile, symbol, (err, tags) => {
if (!tags) {
tags = [];
}
if (err) {
foundErr = err;
doneDetect(false);
} else if (tags.length) {
for (const tag of Array.from(tags)) {
tag.directory = projectPath;
}
foundTags = tags;
doneDetect(true);
} else {
doneDetect(false);
}
});
}, detectCallback);
}, (err, foundTags) => {
callback(err, _.flatten(foundTags));
});
},
getAllTags(callback) {
const projectTags = [];
const task = Task.once(handlerPath, atom.project.getPaths(), () => callback(projectTags));
task.on('tags', (tags) => {
projectTags.push(...tags);
});
return task;
},
};