-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathjavascript.js
More file actions
110 lines (95 loc) · 3.68 KB
/
javascript.js
File metadata and controls
110 lines (95 loc) · 3.68 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
define(function (require, exports, module) {
const QuickOpen = require("search/QuickOpen"),
QuickOpenHelper = require("search/QuickOpenHelper"),
JSUtils = require("language/JSUtils"),
DocumentManager = require("document/DocumentManager"),
StringMatch = require("utils/StringMatch");
/**
* FileLocation class
* @constructor
* @param {string} fullPath
* @param {number} line
* @param {number} chFrom column start position
* @param {number} chTo column end position
* @param {string} functionName
*/
function FileLocation(fullPath, line, chFrom, chTo, functionName) {
this.fullPath = fullPath;
this.line = line;
this.chFrom = chFrom;
this.chTo = chTo;
this.functionName = functionName;
}
/**
* Contains a list of information about functions for a single document.
*
* @return {?Array.<FileLocation>}
*/
function createFunctionList() {
var doc = DocumentManager.getCurrentDocument();
if (!doc) {
return;
}
var functionList = [];
var docText = doc.getText();
var functions = JSUtils.findAllMatchingFunctionsInText(docText, "*");
functions.forEach(function (funcEntry) {
functionList.push(new FileLocation(null, funcEntry.nameLineStart, funcEntry.columnStart, funcEntry.columnEnd, funcEntry.label || funcEntry.name));
});
return functionList;
}
/**
* @param {string} query what the user is searching for
* @param {StringMatch.StringMatcher} matcher object that caches search-in-progress data
* @return {Array.<SearchResult>} sorted and filtered results that match the query
*/
function search(query, matcher) {
var functionList = matcher.functionList;
if (!functionList) {
functionList = createFunctionList();
matcher.functionList = functionList;
}
query = query.slice(query.indexOf("@") + 1, query.length);
// Filter and rank how good each match is
var filteredList = $.map(functionList, function (fileLocation) {
var searchResult = matcher.match(fileLocation.functionName, query);
if (searchResult) {
searchResult.fileLocation = fileLocation;
}
return searchResult;
});
// Sort based on ranking & basic alphabetical order
StringMatch.basicMatchSort(filteredList);
return filteredList;
}
QuickOpen.addQuickOpenPlugin(
{
name: "JavaScript functions",
languageIds: ["javascript"],
search: search,
match: QuickOpenHelper.match,
itemFocus: QuickOpenHelper.itemFocus,
itemSelect: QuickOpenHelper.itemSelect
}
);
});