Skip to content

Commit eca5ac6

Browse files
committed
Build the PG operator snippets list at initialization.
This is so that when a completion is needed this list can just be added to the available completion options instead of building it each time a completion is needed. Also add the WWPlot objects (Label, Circle, and Fun) to the completion list.
1 parent c32f4af commit eca5ac6

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

src/pg-parser.ts

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@ import type { SyntaxNode } from '@lezer/common';
55
import { parser } from './pg.grammar';
66
import { pgVariables, pgOperators, pgOperatorCompletions } from './pg-variables';
77

8+
const pgOperatorCompletionOptions: Completion[] = [];
9+
10+
for (const operator of pgOperators.values()) {
11+
const completions = pgOperatorCompletions.get(operator);
12+
if (completions) {
13+
for (const template of completions) {
14+
pgOperatorCompletionOptions.push(
15+
snippetCompletion(template, {
16+
label: operator,
17+
info: template
18+
.replaceAll(/\${([^}]*)}/g, '$1')
19+
.replaceAll(/\t/g, '')
20+
.replaceAll(/\n/g, ' '),
21+
type: 'variable',
22+
section: { name: 'PG Methods', rank: 1 }
23+
})
24+
);
25+
}
26+
} else {
27+
pgOperatorCompletionOptions.push(
28+
snippetCompletion(`${operator}(\${})\${}`, {
29+
label: operator,
30+
info: `${operator}()`,
31+
type: 'variable',
32+
section: { name: 'PG Methods', rank: 1 }
33+
})
34+
);
35+
}
36+
}
37+
38+
for (const pkg of ['Scaffold', 'Section']) {
39+
for (const position of ['Begin', 'End']) {
40+
pgOperatorCompletionOptions.push(
41+
snippetCompletion(`${pkg}::${position}(\${});\${}`, {
42+
label: `${pkg}::${position}`,
43+
info: `${pkg}::${position}();`,
44+
type: 'variable',
45+
section: { name: 'PG Methods', rank: 1 }
46+
})
47+
);
48+
}
49+
}
50+
51+
for (const pkg of ['Label', 'Circle', 'Fun']) {
52+
pgOperatorCompletionOptions.push({ label: pkg, type: 'variable', section: { name: 'WWPlot Object', rank: 2 } });
53+
}
54+
855
export const pgCompletion = (isTop = false) => {
956
return (context: CompletionContext) => {
1057
const nodeAt = syntaxTree(context.state).resolveInner(context.pos, -1);
@@ -78,48 +125,15 @@ export const pgCompletion = (isTop = false) => {
78125

79126
if (
80127
inside(['CallExpression', 'FunctionName', 'Identifier']) &&
81-
!inside(['StringSingleQuoted', 'StringQQuoted', 'InterpolatedStringContent', 'UninterpolatedHeredocBody'])
128+
!inside([
129+
'StringSingleQuoted',
130+
'StringQQuoted',
131+
'InterpolatedStringContent',
132+
'UninterpolatedHeredocBody',
133+
'MethodInvocation'
134+
])
82135
) {
83-
for (const operator of pgOperators.values()) {
84-
const completions = pgOperatorCompletions.get(operator);
85-
if (completions) {
86-
for (const template of completions) {
87-
completionOptions.push(
88-
snippetCompletion(template, {
89-
label: operator,
90-
info: template
91-
.replaceAll(/\${([^}]*)}/g, '$1')
92-
.replaceAll(/\t/g, '')
93-
.replaceAll(/\n/g, ' '),
94-
type: 'variable',
95-
section: { name: 'PG Methods' }
96-
})
97-
);
98-
}
99-
} else {
100-
completionOptions.push(
101-
snippetCompletion(`${operator}(\${})\${}`, {
102-
label: operator,
103-
info: `${operator}()`,
104-
type: 'variable',
105-
section: { name: 'PG Methods' }
106-
})
107-
);
108-
}
109-
}
110-
111-
for (const part of ['Scaffold', 'Section']) {
112-
for (const position of ['Begin', 'End']) {
113-
completionOptions.push(
114-
snippetCompletion(`${part}::${position}(\${});\${}`, {
115-
label: `${part}::${position}`,
116-
info: `${part}::${position}();`,
117-
type: 'variable',
118-
section: { name: 'PG Methods' }
119-
})
120-
);
121-
}
122-
}
136+
completionOptions.push(...pgOperatorCompletionOptions);
123137
}
124138

125139
return completeFromList(completionOptions)(context);

0 commit comments

Comments
 (0)