-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathautoCompleteProvider.js
106 lines (106 loc) · 4.45 KB
/
autoCompleteProvider.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var parent = require("../../worker/parent");
var fs = require("fs");
var atomUtils = require("./atomUtils");
var fuzzaldrin = require('fuzzaldrin');
var CSON = require("season");
var explicitlyTriggered = false;
function triggerAutocompletePlus() {
atom.commands.dispatch(atom.views.getView(atom.workspace.getActiveTextEditor()), 'autocomplete-plus:activate');
explicitlyTriggered = true;
}
exports.triggerAutocompletePlus = triggerAutocompletePlus;
function getModuleAutocompleteType(scopes) {
function has(match) {
return scopes.some(function (scope) { return scope.indexOf(match) !== -1; });
}
var isString = has('string.quoted');
return {
isReference: has('reference.path.string.quoted') || has('amd.path.string.quoted'),
isRequire: has('meta.import-equals.external') && isString,
isImport: has('meta.import') && isString
};
}
exports.provider = {
selector: '.source.ts, .source.tsx',
inclusionPriority: 3,
suggestionPriority: 3,
excludeLowerPriority: false,
getSuggestions: function (options) {
var filePath = options.editor.getPath();
if (!filePath)
return Promise.resolve([]);
if (!fs.existsSync(filePath))
return Promise.resolve([]);
var _a = getModuleAutocompleteType(options.scopeDescriptor.scopes), isReference = _a.isReference, isRequire = _a.isRequire, isImport = _a.isImport;
if (isReference || isRequire || isImport) {
return parent.getRelativePathsInProject({ filePath: filePath, prefix: options.prefix, includeExternalModules: isReference })
.then(function (resp) {
var range = options.editor.bufferRangeForScopeAtCursor(".string.quoted");
var cursor = options.editor.getCursorBufferPosition();
if (!range || cursor.column !== range.end.column - 1) {
return [];
}
var content = options.editor.getTextInBufferRange(range).replace(/^['"]|['"]$/g, "");
return resp.files.map(function (file) {
var relativePath = file.relativePath;
var suggestionText = relativePath;
var suggestion = {
text: suggestionText,
replacementPrefix: content,
rightLabelHTML: '<span>' + file.name + '</span>',
type: 'import'
};
return suggestion;
});
});
}
else {
if (explicitlyTriggered) {
explicitlyTriggered = false;
}
else {
var prefix = options.prefix.trim();
if (prefix === '' || prefix === ';' || prefix === '{') {
return Promise.resolve([]);
}
}
var position = atomUtils.getEditorPositionForBufferPosition(options.editor, options.bufferPosition);
var promisedSuggestions = parent.getCompletionsAtPosition({
filePath: filePath,
position: position,
prefix: options.prefix,
})
.then(function (resp) {
var completionList = resp.completions;
var suggestions = completionList.map(function (c) {
if (c.snippet) {
return {
snippet: c.snippet,
replacementPrefix: '',
rightLabel: 'signature',
type: 'snippet',
};
}
else {
var prefix = options.prefix;
if (c.name && c.name.startsWith('$')) {
prefix = "$" + prefix;
}
return {
text: c.name,
replacementPrefix: resp.endsInPunctuation ? '' : prefix.trim(),
rightLabel: c.display,
leftLabel: c.kind,
type: atomUtils.kindToType(c.kind),
description: c.comment,
};
}
});
return suggestions;
});
return promisedSuggestions;
}
},
};