-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
131 lines (118 loc) · 5.49 KB
/
main.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
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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
MultiRangeInlineEditor = brackets.getModule("editor/MultiRangeInlineEditor").MultiRangeInlineEditor,
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
FileUtils = brackets.getModule("file/FileUtils"),
Async = brackets.getModule("utils/Async"),
TypeScriptUtils = require("TypeScript/main").TypeScriptUtils,
TypeScriptService = require("TypeScript/main").TypeScriptService;
/**
* @private
* For unit and performance tests. Allows lookup by function name instead of editor offset .
*
* @param {!Editor} hostEditor
* @param {!string} functionName
* @return {$.Promise} a promise that will be resolved with an InlineWidget
* or null if we're not going to provide anything.
*/
function _createInlineEditor(hostEditor, declInfo) {
var result = new $.Deferred();
DocumentManager.getDocumentForPath(declInfo.scriptName)
.done(function (doc) {
var functions = [{
document: doc,
lineStart: declInfo.range.start.line,
lineEnd: declInfo.range.end.line,
name: declInfo.name
}];
//TODO: use a one range inline editor if there is always one result?
var jsInlineEditor = new MultiRangeInlineEditor(functions);
jsInlineEditor.load(hostEditor);
result.resolve(jsInlineEditor);
})
.fail(function (error) {
result.reject(error);
});
return result.promise();
}
/**
* This function is registered with EditorManager as an inline editor provider. It creates an inline editor
* when the cursor is on a TypeScript function name, finds all functions that match the name
* and shows (one/all of them) in an inline editor.
*
* @param {!Editor} editor
* @param {!{line:Number, ch:Number}} pos
* @return {$.Promise} a promise that will be resolved with an InlineWidget
* or null if we're not going to provide anything.
*/
function typeScriptFunctionProvider(hostEditor, pos) {
var result = new $.Deferred();
// Only provide a TypeScript editor when cursor is in TypeScript content
var languageId = hostEditor.getLanguageForSelection().getId();
if (languageId !== TypeScriptUtils.LANGUAGE_ID) {
return null;
}
// Only provide TypeScript editor if the selection is within a single line
var sel = hostEditor.getSelection(false);
if (sel.start.line !== sel.end.line) {
return null;
}
TypeScriptService.getSession(hostEditor.document).done(function (session) {
var hostTsDoc = session.tsDoc;
var symbol = hostTsDoc.getSymbolAtPosition(sel.start);
var declInfo = hostTsDoc.getDeclarationInfo(symbol);
// Cancel if there is no declaration associated to the current selection
if (!declInfo) {
result.reject();
return;
}
// Cancel if the declaration is at the same line as the current selection
if (declInfo.scriptName === hostTsDoc.scriptName) {
var line = hostEditor.getSelection(false).start.line;
if (declInfo.range.start.line === line) {
result.reject();
return;
}
}
_createInlineEditor(hostEditor, declInfo)
.done(function (inlineEditor) {
result.resolve(inlineEditor);
})
.fail(function (error) {
result.reject(error);
});
});
return result;
}
AppInit.appReady(function () {
EditorManager.registerInlineEditProvider(typeScriptFunctionProvider);
// For unit testing
exports.typeScriptFunctionProvider = typeScriptFunctionProvider;
});
});