Skip to content

Commit 7b95065

Browse files
committed
fix completion and verification
1 parent 734a72c commit 7b95065

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

src/features/constraintMenu/AutoCompletion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export class AutoCompleteTree {
233233
return result;
234234
}
235235
for (const n of nodes) {
236-
if (!n.word.verifyWord(tokens[index].text)) {
236+
if (n.word.verifyWord(tokens[index].text).length > 0) {
237237
continue;
238238
}
239239
result = result.concat(this.completeNode(n.children, tokens, index + 1));

src/features/dfdElements/AssignmentLanguage.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export namespace TreeBuilder {
133133
word: new ConstantWord("from"),
134134
children: [
135135
{
136-
word: new InputWord(model),
136+
word: new InputListWord(model),
137137
children: [],
138138
},
139139
],
@@ -221,17 +221,11 @@ class LabelWord implements ReplaceableAbstractWord {
221221
return [];
222222
}
223223

224-
const possibleValues: WordCompletion[] = type.values.map((l) => ({
224+
return type.values.map((l) => ({
225225
insertText: l.text,
226226
kind: monaco.languages.CompletionItemKind.Enum,
227227
startOffset: parts[0].length + 1,
228228
}));
229-
possibleValues.push({
230-
insertText: "$" + type.name,
231-
kind: monaco.languages.CompletionItemKind.Variable,
232-
startOffset: parts[0].length + 1,
233-
});
234-
return possibleValues;
235229
}
236230

237231
return [];
@@ -333,8 +327,20 @@ class InputListWord implements ReplaceableAbstractWord {
333327
this.inputWord = new InputWord(model);
334328
}
335329

336-
completionOptions(): WordCompletion[] {
337-
return this.inputWord.completionOptions();
330+
completionOptions(word: string): WordCompletion[] {
331+
const parts = word.split(",");
332+
// remove last one as we are completing that one
333+
if (parts.length > 1) {
334+
parts.pop();
335+
}
336+
const startOffset = parts.reduce((acc, part) => acc + part.length + 1, 0); // +1 for the commas
337+
return this.inputWord
338+
.completionOptions()
339+
.filter((c) => !parts.includes(c.insertText))
340+
.map((c) => ({
341+
...c,
342+
startOffset: startOffset + (c.startOffset ?? 0),
343+
}));
338344
}
339345

340346
verifyWord(word: string): string[] {
@@ -363,13 +369,17 @@ class InputLabelWord implements ReplaceableAbstractWord {
363369
}
364370

365371
completionOptions(word: string): WordCompletion[] {
366-
const parts = word.split(".");
372+
const parts = this.getParts(word);
367373
if (parts[1] === undefined) {
368-
return this.inputWord.completionOptions();
369-
} else if (parts.length === 2) {
374+
return this.inputWord.completionOptions().map((c) => ({
375+
...c,
376+
insertText: c.insertText,
377+
}));
378+
} else if (parts.length >= 2) {
370379
return this.labelWord.completionOptions(parts[1]).map((c) => ({
371380
...c,
372-
insertText: parts[0] + "." + c.insertText,
381+
insertText: c.insertText,
382+
startOffset: (c.startOffset ?? 0) + parts[0].length + 1, // +1 for the dot
373383
}));
374384
}
375385
return [];

src/features/dfdElements/outputPortEditUi.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
101101
private port: DfdOutputPortImpl | undefined;
102102
private editor?: monaco.editor.IStandaloneCodeEditor;
103103
private tree?: ReplaceAutoCompleteTree;
104+
private completionProvider?: monaco.IDisposable;
105+
106+
private static readonly DFD_LANGUAGE_NAME = "dfd-behavior";
104107

105108
constructor(
106109
@inject(TYPES.IActionDispatcher) private actionDispatcher: ActionDispatcher,
@@ -145,15 +148,13 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
145148
this.validationLabel.classList.add("validation-label");
146149

147150
// Initialize the monaco editor and setup the language for highlighting and autocomplete.
148-
const dfdLanguageName = "dfd-behavior";
149-
monaco.languages.register({ id: dfdLanguageName });
150-
monaco.languages.setMonarchTokensProvider(dfdLanguageName, assignemntLanguageMonarchDefinition);
151-
if (this.tree) {
152-
monaco.languages.registerCompletionItemProvider(
153-
dfdLanguageName,
154-
new MonacoEditorAssignmentLanguageCompletionProvider(this.tree),
155-
);
156-
}
151+
152+
monaco.languages.register({ id: OutputPortEditUI.DFD_LANGUAGE_NAME });
153+
monaco.languages.setMonarchTokensProvider(
154+
OutputPortEditUI.DFD_LANGUAGE_NAME,
155+
assignemntLanguageMonarchDefinition,
156+
);
157+
this.registerCompletionProvider();
157158

158159
const monacoTheme = (ThemeManager?.useDarkMode ?? true) ? "vs-dark" : "vs";
159160
this.editor = monaco.editor.create(this.editorContainer, {
@@ -166,7 +167,7 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
166167
wordBasedSuggestions: "off", // Does not really work for our use case
167168
scrollBeyondLastLine: false, // Not needed
168169
theme: monacoTheme,
169-
language: dfdLanguageName,
170+
language: OutputPortEditUI.DFD_LANGUAGE_NAME,
170171
});
171172

172173
this.configureHandlers(containerElement);
@@ -213,7 +214,9 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
213214
});
214215

215216
// Run behavior validation when the behavior text changes.
216-
this.editor?.onDidChangeModelContent(() => {});
217+
this.editor?.onDidChangeModelContent(() => {
218+
this.validateBehavior();
219+
});
217220

218221
// When the content size of the editor changes, resize the editor accordingly.
219222
this.editor?.onDidContentSizeChange(() => {
@@ -320,6 +323,8 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
320323
// Validation of loaded behavior text.
321324
this.validateBehavior();
322325

326+
this.registerCompletionProvider();
327+
323328
// Wait for the next event loop tick to focus the port edit UI.
324329
// The user may have clicked more times before the show click was processed
325330
// (showing the UI takes some time due to finding the element in the graph, etc.).
@@ -330,6 +335,17 @@ export class OutputPortEditUI extends AbstractUIExtension implements Switchable
330335
}, 0); // 0ms => next event loop tick
331336
}
332337

338+
private registerCompletionProvider() {
339+
if (!this.tree) {
340+
return;
341+
}
342+
this.completionProvider?.dispose();
343+
this.completionProvider = monaco.languages.registerCompletionItemProvider(
344+
OutputPortEditUI.DFD_LANGUAGE_NAME,
345+
new MonacoEditorAssignmentLanguageCompletionProvider(this.tree),
346+
);
347+
}
348+
333349
/**
334350
* Sets the position of the UI to the position of the port that is currently edited.
335351
*/

0 commit comments

Comments
 (0)