-
Notifications
You must be signed in to change notification settings - Fork 10
/
extension.js
126 lines (102 loc) · 3.17 KB
/
extension.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
const vscode = require("vscode");
const {
currentFilePath,
currentWord,
syncFileContents
} = require("./lib/project");
const { start, kill, run, on } = require("./lib/daemon");
const STATUS_BAR_DELAY = 3000;
function handleMessage(json) {
const { messages, fileContent, unresolvedImports, error, goto } = json;
if (error) {
vscode.window.showWarningMessage(
`ImportJS encountered an error: ${error.message}`
);
return;
}
if (messages && messages.length > 0) {
vscode.window.setStatusBarMessage(messages.join("\n"), STATUS_BAR_DELAY);
}
if (goto) {
vscode.workspace.openTextDocument(goto).then(document => {
vscode.window.showTextDocument(document);
});
return;
}
// Always sync resolved imports, even if there are some remaining to resolve
if ("fileContent" in json) {
syncFileContents(fileContent);
}
if (unresolvedImports && Object.keys(unresolvedImports).length > 0) {
const imports = Object.keys(unresolvedImports).map(name => {
return { name, options: unresolvedImports[name] };
});
// Ask the user to resolve each import, one at a time. When all imports are
// resolved, or when the user cancels, the chain of promises will resolve.
const requestResolutions = (remaining, resolutions = []) => {
if (remaining.length === 0) {
return Promise.resolve(resolutions);
}
const { name, options } = remaining[0];
let pickerItems = options
// Filter out duplicates
.reduce((uniqueImports, option) => {
const duplicate = uniqueImports.find(
({ displayName }) => option.displayName === displayName
);
if (duplicate) {
return uniqueImports;
}
return uniqueImports.concat(option);
}, [])
.map(({ displayName, data }) => {
return {
label: displayName,
data
};
});
return vscode.window.showQuickPick(pickerItems).then(selected => {
// If user cancels, still import the modules they've resolved so far
if (!selected) {
return Promise.resolve(resolutions);
}
const { data } = selected;
const resolution = {
name,
data
};
return requestResolutions(
remaining.slice(1),
resolutions.concat(resolution)
);
});
};
requestResolutions(imports).then(resolutions => {
const imports = resolutions.reduce((imports, resolution) => {
const { name, data } = resolution;
imports[name] = data;
return imports;
}, {});
run("add", imports);
});
}
}
function activate(context) {
const subscriptions = [
vscode.commands.registerCommand("importjs.word", () =>
run("word", currentWord())
),
vscode.commands.registerCommand("importjs.goto", () =>
run("goto", currentWord())
),
vscode.commands.registerCommand("importjs.fix", () => run("fix"))
];
subscriptions.forEach(sub => context.subscriptions.push(sub));
on("message", handleMessage);
start();
}
function deactivate() {
kill();
}
exports.activate = activate;
exports.deactivate = deactivate;