generated from lifeart/els-a11y-addon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (101 loc) · 3.17 KB
/
index.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
"use strict";
const { TextEdit, Position, Command } = require("vscode-languageserver");
const { URI } = require("vscode-uri");
function normalizeToAngleBracketComponent(name) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;
if (name.includes(".")) {
return name;
}
return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === "/") {
return "::";
}
if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toUpperCase();
}
// Remove all occurrences of '-'s from the name that aren't starting with `-`
return char === "-" ? "" : char.toLowerCase();
});
}
module.exports = {
onInit(_, project) {
if (!("els.executeInEmberCLI" in project.executors)) {
console.error('Unable to find "ember-fast-cli" addon.');
return;
}
project.executors["els.extractSourceCodeToComponent"] = async (
server,
filePath,
[rawComponentName, { range, source, uri }]
) => {
if (!rawComponentName.trim()) {
console.log("no component name found");
return;
}
try {
// const ast = server.templateCompletionProvider.getAST(document.getText(range));
// const focusPath = server.templateCompletionProvider.createFocusPath(ast, ast.loc.start, text);
await server.onExecute({
command: "els.executeInEmberCLI",
arguments: [filePath, `g component ${rawComponentName}`],
});
const componentName = rawComponentName.trim().split(" ").pop();
// going to wait for file changes api
await new Promise((resolve) => setTimeout(resolve, 2000));
const registry = server.getRegistry(project.root);
if (!(componentName in registry.component)) {
console.log(
`Unable to find component ${componentName} in registry ${JSON.stringify(
Object.keys(registry.component)
)}`
);
return;
}
const fileName = registry["component"][componentName].find((file) =>
file.endsWith(".hbs")
);
if (!fileName) {
console.log(
`Unable to find template file for component ${componentName}`
);
return;
}
const fileUri = URI.file(fileName).toString();
const edit = {
changes: {
[uri]: [
TextEdit.replace(
range,
`<${normalizeToAngleBracketComponent(componentName)} />`
),
],
[fileUri]: [TextEdit.insert(Position.create(0, 0), source)],
},
};
await server.connection.workspace.applyEdit(edit);
} catch (e) {
console.error(e);
}
};
},
onCodeAction(_, params) {
if (!params.textDocument.uri.endsWith(".hbs")) {
return;
}
const act = Command.create(
"Extract selection to component",
"els.getUserInput",
{
placeHolder: "Enter component name",
},
"els.extractSourceCodeToComponent",
{
source: params.document.getText(params.range),
range: params.range,
uri: params.textDocument.uri,
}
);
return [act];
},
};