-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add autocomplete and computed grammar (#199)
* feat: add autocomplete for some Given statement examples * chore: change to snippet completition * feat: autogenerate list of autocomplete statements * ci: now grammar build is not automatic with packages installation * fix: include missing file * chore: add missing license headers * add pnpm build to build:grammar * feat: add case Insensitive grammar and syntax highlighting * chore: make autocomplete case insensitive * fix: fix error in then autocomplete * chore: add autocomplete for then statements * fix: add G for complete Given statements * fix(grammar): add missing single quotes * ci(grammar): use correct command to build grammar package * chore: apply prettier to grammar code --------- Co-authored-by: FilippoTrotter <[email protected]>
- Loading branch information
1 parent
51a25b5
commit b710a04
Showing
20 changed files
with
1,247 additions
and
707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-FileCopyrightText: 2024 Dyne.org foundation | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
import { CompletionContext, snippetCompletion } from '@codemirror/autocomplete'; | ||
import { fullStatementTemplates } from './complete_statement'; | ||
|
||
// Helper function to strip quotes from matched strings | ||
function stripQuotes(s: string) { | ||
return s.replace(/^'|'$/g, ''); | ||
} | ||
|
||
const fullStatementSnippets = fullStatementTemplates.map((x) => { | ||
let n = 1; | ||
return snippetCompletion( | ||
x.label.replace(/''/g, () => `'\${${n++}:}'`), | ||
x, | ||
); | ||
}); | ||
|
||
export function completeStatement(context: CompletionContext) { | ||
const line = context.state.doc.lineAt(context.pos); | ||
let textBefore = context.state.sliceDoc(line.from, context.pos); | ||
const triggerMatch = /^[GT].*$/i.exec(textBefore); | ||
|
||
if (triggerMatch) { | ||
const strings = textBefore.match(/'([^']*)'/g); | ||
textBefore = textBefore.toLowerCase(); | ||
if (!strings) { | ||
return { | ||
from: context.pos - triggerMatch[0].length, | ||
options: fullStatementSnippets, | ||
validFor: /^.*$/, | ||
}; | ||
} | ||
|
||
const strippedStrings = strings.map(stripQuotes); | ||
|
||
const templateOption = fullStatementTemplates.map((x) => { | ||
let n = 1; | ||
let m = 0; | ||
return snippetCompletion( | ||
x.label.replace(/''/g, () => `'\${${n}:${strippedStrings[n++ - 1] || ''}}'`), | ||
{ | ||
label: x.label.replace(/''/g, () => `${strings[m++] || "''"}`), | ||
type: x.type, | ||
}, | ||
); | ||
}); | ||
|
||
return { | ||
from: context.pos - textBefore.length, | ||
options: templateOption, | ||
validFor: /^.*$/, | ||
}; | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.