Repository navigation
Expand file tree
/
Copy pathselectionMenu.js
More file actions
200 lines (187 loc) · 5.43 KB
/
Copy pathselectionMenu.js
File metadata and controls
200 lines (187 loc) · 5.43 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { focusEditorIfEditable } from "cm/editorReadOnly";
import appSettings from "lib/settings";
const exec = (command) => {
const { editor } = editorManager;
editor.execCommand(command);
if (command === "selectall") {
editor.scrollToRow(Number.POSITIVE_INFINITY);
editor.setSelection(true);
editor.setMenu(true);
}
focusEditorIfEditable(editor);
};
const showCodeActions = async () => {
const { editor } = editorManager;
if (!editor) return;
try {
const { showCodeActionsMenu, supportsCodeActions } = await import("cm/lsp");
if (supportsCodeActions(editor)) {
await showCodeActionsMenu(editor);
}
} catch (error) {
console.warn("[SelectionMenu] Code actions not available:", error);
}
};
const showLspActions = async () => {
const { editor } = editorManager;
if (!editor) return;
try {
const lsp = await import("cm/lsp");
const { LSPPlugin } = await import("@codemirror/lsp-client");
const hasCapability = (feature, capability) =>
LSPPlugin.getAll(editor, feature).some(
(plugin) => !!plugin.client.serverCapabilities?.[capability],
);
const editable = !editor.state.readOnly;
const actions = [
hasCapability("definition", "definitionProvider") && {
value: "definition",
text: getLabel("go to definition", "Go to Definition"),
icon: "keyboard_arrow_right",
run: lsp.goToDefinition,
},
hasCapability("declaration", "declarationProvider") && {
value: "declaration",
text: getLabel("go to declaration", "Go to Declaration"),
icon: "keyboard_arrow_right",
run: lsp.goToDeclaration,
},
hasCapability("implementation", "implementationProvider") && {
value: "implementation",
text: getLabel("go to implementation", "Go to Implementation"),
icon: "keyboard_arrow_right",
run: lsp.goToImplementation,
},
hasCapability("typeDefinition", "typeDefinitionProvider") && {
value: "type-definition",
text: getLabel("go to type definition", "Go to Type Definition"),
icon: "keyboard_arrow_right",
run: lsp.goToTypeDefinition,
},
hasCapability("references", "referencesProvider") && {
value: "references",
text: getLabel("find references", "Find References"),
icon: "linkinsert_link",
run: lsp.findAllReferences,
},
editable &&
hasCapability("rename", "renameProvider") && {
value: "rename",
text: getLabel("rename symbol", "Rename Symbol"),
icon: "edit",
run: lsp.renameSymbol,
},
].filter(Boolean);
if (!actions.length) return;
// Let the tap/click that opened this picker finish before its rows exist.
// Otherwise Android WebView can deliver that click to the first row.
await new Promise((resolve) => setTimeout(resolve, 0));
const { default: select } = await import("dialogs/select");
const selected = await select(
getLabel("lsp actions", "LSP Actions"),
actions,
).catch(() => null);
const action = actions.find((item) => item.value === selected);
if (action) await action.run(editor);
} catch (error) {
console.warn("[SelectionMenu] LSP actions not available:", error);
}
};
const items = [];
export default function selectionMenu(options = {}) {
const { codeActionsAvailable = true, lspActionsAvailable = true } = options;
return [
item(
() => exec("copy"),
<span className="icon copy"></span>,
"selected",
true,
{ id: "copy", label: getLabel("copy", "Copy") },
),
item(
() => exec("cut"),
<span className="icon cut"></span>,
"selected",
false,
{
id: "cut",
label: getLabel("cut", "Cut"),
},
),
item(
() => exec("paste"),
<span className="icon paste"></span>,
"all",
false,
{
id: "paste",
label: getLabel("paste", "Paste"),
},
),
item(
() => exec("selectall"),
<span className="icon text_format"></span>,
"all",
true,
{ id: "select-all", label: getLabel("select all", "Select all") },
),
appSettings.get("showShareButton") &&
item(
() => exec("share"),
<span className="icon share"></span>,
"selected",
true,
{ id: "share", label: getLabel("share", "Share") },
),
item(
(color) => acode.exec("insert-color", color),
<span className="icon color_lenspalette"></span>,
"all",
false,
{ id: "insert-color", label: getLabel("insert color", "Insert color") },
),
codeActionsAvailable &&
item(
() => showCodeActions(),
<span className="icon lightbulb"></span>,
"all",
true,
{
id: "code-actions",
label: getLabel("code actions", "Code Actions"),
},
),
lspActionsAvailable &&
item(
() => showLspActions(),
<span className="icon zap"></span>,
"all",
true,
{
id: "lsp-actions",
label: getLabel("lsp actions", "LSP Actions"),
},
),
...items,
].filter(Boolean);
}
/**
*
* @param {function} onclick function to be called when the item is clicked
* @param {string | HTMLElement} text content of the item
* @param {'selected'|'all'} mode mode supported by the item
* @param {boolean} readOnly whether to show the item in readOnly mode
* @param {{id?: string, label?: string}} options display metadata
*/
selectionMenu.add = (onclick, text, mode, readOnly, options) => {
items.push(item(onclick, text, mode, readOnly, options));
};
selectionMenu.exec = (command) => {
exec(command);
};
function item(onclick, text, mode = "all", readOnly = false, options = {}) {
return { onclick, text, mode, readOnly, ...options };
}
function getLabel(key, fallback) {
return globalThis.strings?.[key] || fallback;
}