diff --git a/examples/domainmodel/src/language-server/domain-model-rename-refactoring.ts b/examples/domainmodel/src/language-server/domain-model-rename-refactoring.ts index b2b24c9ca..106905207 100644 --- a/examples/domainmodel/src/language-server/domain-model-rename-refactoring.ts +++ b/examples/domainmodel/src/language-server/domain-model-rename-refactoring.ts @@ -9,7 +9,7 @@ import type { Range, WorkspaceEdit } from 'vscode-languageserver'; import type { RenameParams } from 'vscode-languageserver-protocol'; import type { DomainModelServices } from './domain-model-module.js'; import type { QualifiedNameProvider } from './domain-model-naming.js'; -import { DefaultRenameProvider, findDeclarationNodeAtOffset, getDocument, isNamed, streamAst, toDocumentSegment } from 'langium'; +import { AstUtils, CstUtils, DefaultRenameProvider, isNamed } from 'langium'; import { Location } from 'vscode-languageserver'; import { TextEdit } from 'vscode-languageserver-types'; import { isPackageDeclaration } from './generated/ast.js'; @@ -30,7 +30,7 @@ export class DomainModelRenameProvider extends DefaultRenameProvider { const rootNode = document.parseResult.value.$cstNode; if (!rootNode) return undefined; const offset = document.textDocument.offsetAt(params.position); - const leafNode = findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp); + const leafNode = CstUtils.findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp); if (!leafNode) return undefined; const targetNode = this.references.findDeclaration(leafNode); if (!targetNode) return undefined; @@ -48,7 +48,7 @@ export class DomainModelRenameProvider extends DefaultRenameProvider { } } - for (const node of streamAst(targetNode)) { + for (const node of AstUtils.streamAst(targetNode)) { const qn = this.buildQualifiedName(node); if (qn) { const options = { onlyLocal: false, includeDeclaration: false }; @@ -88,10 +88,10 @@ export class DomainModelRenameProvider extends DefaultRenameProvider { getNodeLocation(targetNode: AstNode): Location | undefined { const nameNode = this.nameProvider.getNameNode(targetNode); if (nameNode) { - const doc = getDocument(targetNode); + const doc = AstUtils.getDocument(targetNode); return Location.create( doc.uri.toString(), - toDocumentSegment(nameNode).range + CstUtils.toDocumentSegment(nameNode).range ); } return undefined; diff --git a/examples/domainmodel/src/language-server/domain-model-scope.ts b/examples/domainmodel/src/language-server/domain-model-scope.ts index aabfab251..02fa7dcaf 100644 --- a/examples/domainmodel/src/language-server/domain-model-scope.ts +++ b/examples/domainmodel/src/language-server/domain-model-scope.ts @@ -8,7 +8,7 @@ import type { AstNode, AstNodeDescription, LangiumDocument, PrecomputedScopes } import type { DomainModelServices } from './domain-model-module.js'; import type { QualifiedNameProvider } from './domain-model-naming.js'; import type { Domainmodel, PackageDeclaration } from './generated/ast.js'; -import { DefaultScopeComputation, interruptAndCheck, MultiMap, streamAllContents } from 'langium'; +import { AstUtils, DefaultScopeComputation, interruptAndCheck, MultiMap } from 'langium'; import { CancellationToken } from 'vscode-jsonrpc'; import { isType, isPackageDeclaration } from './generated/ast.js'; @@ -26,7 +26,7 @@ export class DomainModelScopeComputation extends DefaultScopeComputation { */ override async computeExports(document: LangiumDocument, cancelToken = CancellationToken.None): Promise { const descr: AstNodeDescription[] = []; - for (const modelNode of streamAllContents(document.parseResult.value)) { + for (const modelNode of AstUtils.streamAllContents(document.parseResult.value)) { await interruptAndCheck(cancelToken); if (isType(modelNode)) { let name = this.nameProvider.getName(modelNode); diff --git a/examples/domainmodel/test/refs-index.test.ts b/examples/domainmodel/test/refs-index.test.ts index c6221eef2..d26eb043e 100644 --- a/examples/domainmodel/test/refs-index.test.ts +++ b/examples/domainmodel/test/refs-index.test.ts @@ -7,7 +7,7 @@ import type { AstNode, LangiumDocument, ReferenceDescription, URI } from 'langium'; import type { Domainmodel } from '../src/language-server/generated/ast.js'; import { describe, expect, test } from 'vitest'; -import { EmptyFileSystem, getDocument } from 'langium'; +import { AstUtils, EmptyFileSystem } from 'langium'; import { parseDocument, setTextDocument } from 'langium/test'; import { TextDocument } from 'vscode-languageserver-textdocument'; import { createDomainModelServices } from '../src/language-server/domain-model-module.js'; @@ -62,7 +62,7 @@ function refString(ref: ReferenceDescription): string { } function nodeRefString(from: AstNode, to: AstNode): string { - return asString(getDocument(from).uri, createPath(from), getDocument(to).uri, createPath(to)); + return asString(AstUtils.getDocument(from).uri, createPath(from), AstUtils.getDocument(to).uri, createPath(to)); } function createPath(node: AstNode): string { diff --git a/packages/langium-cli/src/generate.ts b/packages/langium-cli/src/generate.ts index 7ba3ea874..9fd96fc17 100644 --- a/packages/langium-cli/src/generate.ts +++ b/packages/langium-cli/src/generate.ts @@ -8,7 +8,7 @@ import type { AstNode, Grammar, LangiumDocument, Mutable } from 'langium'; import type { LangiumConfig, LangiumLanguageConfig} from './package.js'; import { URI } from 'langium'; import { loadConfig } from './package.js'; -import { copyAstNode, getDocument, GrammarAST, linkContentToContainer } from 'langium'; +import { AstUtils, GrammarAST } from 'langium'; import { createLangiumGrammarServices, resolveImport, resolveImportUri, resolveTransitiveImports } from 'langium/grammar'; import { NodeFileSystem } from 'langium/node'; import { generateAst } from './generator/ast-generator.js'; @@ -148,7 +148,7 @@ async function eagerLoad(document: LangiumDocument, uris: Set = new Set( */ function mapGrammarElements(grammars: Grammar[], visited: Set = new Set(), map: Map = new Map()): Map { for (const grammar of grammars) { - const doc = getDocument(grammar); + const doc = AstUtils.getDocument(grammar); const uriString = doc.uri.toString(); if (!visited.has(uriString)) { visited.add(uriString); @@ -172,7 +172,7 @@ function embedReferencedGrammar(grammar: Grammar, map: Map { await documentBuilder.update([], documents.map(e => e.uri)); // Create and build new documents const newDocuments = grammars.map(e => { - const uri = getDocument(e).uri; + const uri = AstUtils.getDocument(e).uri; const newDoc = documentFactory.fromModel(e, uri); (e as Mutable).$document = newDoc; return newDoc; diff --git a/packages/langium-cli/src/generator/ast-generator.ts b/packages/langium-cli/src/generator/ast-generator.ts index 41c71b610..ce6a01fdb 100644 --- a/packages/langium-cli/src/generator/ast-generator.ts +++ b/packages/langium-cli/src/generator/ast-generator.ts @@ -7,7 +7,7 @@ import type { Grammar, LangiumServices } from 'langium'; import { type Generated, expandToNode, joinToNode, toString } from 'langium/generate'; import type { AstTypes, Property, PropertyDefaultValue } from 'langium/grammar'; import type { LangiumConfig } from '../package.js'; -import { streamAllContents, MultiMap, GrammarAST } from 'langium'; +import { AstUtils, MultiMap, GrammarAST } from 'langium'; import { collectAst, collectTypeHierarchy, findReferenceTypes, isAstType, mergeTypesAndInterfaces } from 'langium/grammar'; import { collectTerminalRegexps, generatedHeader } from './util.js'; @@ -37,7 +37,7 @@ export function generateAst(services: LangiumServices, grammars: Grammar[], conf } function hasCrossReferences(grammar: Grammar): boolean { - return Boolean(streamAllContents(grammar).find(GrammarAST.isCrossReference)); + return Boolean(AstUtils.streamAllContents(grammar).find(GrammarAST.isCrossReference)); } function generateAstReflection(config: LangiumConfig, astTypes: AstTypes): Generated { diff --git a/packages/langium-cli/src/generator/highlighting/monarch-generator.ts b/packages/langium-cli/src/generator/highlighting/monarch-generator.ts index ddad12758..978b0275c 100644 --- a/packages/langium-cli/src/generator/highlighting/monarch-generator.ts +++ b/packages/langium-cli/src/generator/highlighting/monarch-generator.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { type Grammar, getTerminalParts, isCommentTerminal, escapeRegExp, GrammarAST, isWhitespaceRegExp, terminalRegex } from 'langium'; +import { type Grammar, GrammarAST, GrammarUtils, RegExpUtils } from 'langium'; import { type Generated, expandToNode, joinToNode, toString } from 'langium/generate'; import type { LangiumLanguageConfig } from '../../package.js'; import { collectKeywords } from '../util.js'; @@ -225,7 +225,7 @@ function prettyPrintLangDef(languageDef: LanguageDefinition): Generated { ${genLanguageDefEntry('keywords', languageDef.keywords)} ${genLanguageDefEntry('operators', languageDef.operators)} ${/* special case, identify symbols via singular regex*/ undefined} - symbols: ${new RegExp(languageDef.symbols.map(escapeRegExp).join('|')).toString()}, + symbols: ${new RegExp(languageDef.symbols.map(RegExpUtils.escapeRegExp).join('|')).toString()}, `; } @@ -315,20 +315,20 @@ function getWhitespaceRules(grammar: Grammar): Rule[] { const rules: Rule[] = []; for (const rule of grammar.rules) { if (GrammarAST.isTerminalRule(rule)) { - const regex = terminalRegex(rule); + const regex = GrammarUtils.terminalRegex(rule); - if (!isCommentTerminal(rule) && !isWhitespaceRegExp(regex)) { + if (!GrammarUtils.isCommentTerminal(rule) && !RegExpUtils.isWhitespace(regex)) { // skip rules that are not comments or whitespace continue; } // token name is either comment or whitespace - const tokenName = isCommentTerminal(rule) ? 'comment' : 'white'; + const tokenName = GrammarUtils.isCommentTerminal(rule) ? 'comment' : 'white'; - const part = getTerminalParts(regex)[0]; + const part = RegExpUtils.getTerminalParts(regex)[0]; // check if this is a comment terminal w/ a start & end sequence (multi-line) - if (part.start !== '' && part.end !== '' && isCommentTerminal(rule)) { + if (part.start !== '' && part.end !== '' && GrammarUtils.isCommentTerminal(rule)) { // state-based comment rule, only add push to jump into it rules.push({ regex: part.start, @@ -356,9 +356,9 @@ function getWhitespaceRules(grammar: Grammar): Rule[] { function getCommentRules(grammar: Grammar): Rule[] { const rules: Rule[] = []; for (const rule of grammar.rules) { - if (GrammarAST.isTerminalRule(rule) && isCommentTerminal(rule)) { + if (GrammarAST.isTerminalRule(rule) && GrammarUtils.isCommentTerminal(rule)) { const tokenName = 'comment'; - const part = getTerminalParts(terminalRegex(rule))[0]; + const part = RegExpUtils.getTerminalParts(GrammarUtils.terminalRegex(rule))[0]; if (part.start !== '' && part.end !== '') { // rules to manage comment start/end // rule order matters @@ -398,10 +398,10 @@ function getCommentRules(grammar: Grammar): Rule[] { function getTerminalRules(grammar: Grammar): Rule[] { const rules: Rule[] = []; for (const rule of grammar.rules) { - if (GrammarAST.isTerminalRule(rule) && !isCommentTerminal(rule)) { - const regex = terminalRegex(rule); + if (GrammarAST.isTerminalRule(rule) && !GrammarUtils.isCommentTerminal(rule)) { + const regex = GrammarUtils.terminalRegex(rule); - if (isWhitespaceRegExp(regex)) { + if (RegExpUtils.isWhitespace(regex)) { // disallow terminal rules that match whitespace continue; } diff --git a/packages/langium-cli/src/generator/highlighting/prism-generator.ts b/packages/langium-cli/src/generator/highlighting/prism-generator.ts index 2bb0e2755..440213a4a 100644 --- a/packages/langium-cli/src/generator/highlighting/prism-generator.ts +++ b/packages/langium-cli/src/generator/highlighting/prism-generator.ts @@ -3,7 +3,7 @@ * This program and the accompanying materials are made available under the * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { terminalRegex, GrammarAST, escapeRegExp, isCommentTerminal, type Grammar } from 'langium'; +import { GrammarAST, type Grammar, GrammarUtils, RegExpUtils } from 'langium'; import { expandToNode, joinToNode, toString, type Generated } from 'langium/generate'; import _ from 'lodash'; import type { LangiumLanguageConfig } from '../../package.js'; @@ -23,26 +23,26 @@ export function generatePrismHighlighting(grammar: Grammar, config: LangiumLangu const terminals = getTerminals(grammar); const modifier = config.caseInsensitive ? 'i' : ''; - const commentTerminals = terminals.filter(isCommentTerminal); + const commentTerminals = terminals.filter(GrammarUtils.isCommentTerminal); if (commentTerminals.length === 1) { highlighter.comment = { - pattern: terminalRegex(commentTerminals[0]).toString(), + pattern: GrammarUtils.terminalRegex(commentTerminals[0]).toString(), greedy: true }; } else if (commentTerminals.length > 0) { highlighter.comment = commentTerminals.map(e => ({ - pattern: terminalRegex(e).toString(), + pattern: GrammarUtils.terminalRegex(e).toString(), greedy: true })); } const stringTerminal = terminals.find(e => e.name.toLowerCase() === 'string'); if (stringTerminal) { highlighter.string = { - pattern: terminalRegex(stringTerminal).toString(), + pattern: GrammarUtils.terminalRegex(stringTerminal).toString(), greedy: true }; } - const filteredKeywords = keywords.filter(e => idRegex.test(e)).sort((a, b) => b.length - a.length).map(escapeRegExp); + const filteredKeywords = keywords.filter(e => idRegex.test(e)).sort((a, b) => b.length - a.length).map(RegExpUtils.escapeRegExp); highlighter.keyword = { pattern: `/\\b(${filteredKeywords.join('|')})\\b/${modifier}` }; diff --git a/packages/langium-cli/src/generator/highlighting/textmate-generator.ts b/packages/langium-cli/src/generator/highlighting/textmate-generator.ts index f35ad4a0d..03c35fd51 100644 --- a/packages/langium-cli/src/generator/highlighting/textmate-generator.ts +++ b/packages/langium-cli/src/generator/highlighting/textmate-generator.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ import type { Grammar } from 'langium'; -import { escapeRegExp, getCaseInsensitivePattern, getTerminalParts, GrammarAST, isCommentTerminal, stream, terminalRegex } from 'langium'; +import { GrammarAST, GrammarUtils, RegExpUtils, stream } from 'langium'; import type { LangiumLanguageConfig } from '../../package.js'; import { collectKeywords } from '../util.js'; @@ -74,8 +74,8 @@ function getRepository(grammar: Grammar, config: LangiumLanguageConfig): Reposit const commentPatterns: Pattern[] = []; let stringEscapePattern: Pattern | undefined; for (const rule of grammar.rules) { - if (GrammarAST.isTerminalRule(rule) && isCommentTerminal(rule)) { - const parts = getTerminalParts(terminalRegex(rule)); + if (GrammarAST.isTerminalRule(rule) && GrammarUtils.isCommentTerminal(rule)) { + const parts = RegExpUtils.getTerminalParts(GrammarUtils.terminalRegex(rule)); for (const part of parts) { if (part.end) { commentPatterns.push({ @@ -144,7 +144,7 @@ function groupKeywords(keywords: string[], caseInsensitive: boolean | undefined) } = { letter: [], leftSpecial: [], rightSpecial: [], special: [] }; keywords.forEach(keyword => { - const keywordPattern = caseInsensitive ? getCaseInsensitivePattern(keyword) : escapeRegExp(keyword); + const keywordPattern = caseInsensitive ? RegExpUtils.getCaseInsensitivePattern(keyword) : RegExpUtils.escapeRegExp(keyword); if (/\w/.test(keyword[0])) { if (/\w/.test(keyword[keyword.length - 1])) { groups.letter.push(keywordPattern); @@ -173,7 +173,7 @@ function getStringPatterns(grammar: Grammar, pack: LangiumLanguageConfig): Patte const stringTerminal = terminals.find(e => e.name.toLowerCase() === 'string'); const stringPatterns: Pattern[] = []; if (stringTerminal) { - const parts = getTerminalParts(terminalRegex(stringTerminal)); + const parts = RegExpUtils.getTerminalParts(GrammarUtils.terminalRegex(stringTerminal)); for (const part of parts) { if (part.end) { stringPatterns.push({ diff --git a/packages/langium-cli/src/generator/util.ts b/packages/langium-cli/src/generator/util.ts index 9b9107c25..79e55c137 100644 --- a/packages/langium-cli/src/generator/util.ts +++ b/packages/langium-cli/src/generator/util.ts @@ -3,7 +3,7 @@ * This program and the accompanying materials are made available under the * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { type Grammar, getAllReachableRules, GrammarAST, stream, streamAllContents, terminalRegex } from 'langium'; +import { AstUtils, type Grammar, GrammarAST, GrammarUtils, stream } from 'langium'; import { type Generated, expandToNode } from 'langium/generate'; import fs from 'fs-extra'; import * as path from 'node:path'; @@ -59,11 +59,11 @@ function getGeneratedHeader(): Generated { export function collectKeywords(grammar: Grammar): string[] { const keywords = new Set(); - const reachableRules = getAllReachableRules(grammar, false); + const reachableRules = GrammarUtils.getAllReachableRules(grammar, false); for (const keyword of stream(reachableRules) .filter(GrammarAST.isParserRule) - .flatMap(rule => streamAllContents(rule).filter(GrammarAST.isKeyword))) { + .flatMap(rule => AstUtils.streamAllContents(rule).filter(GrammarAST.isKeyword))) { keywords.add(keyword.value); } @@ -97,10 +97,10 @@ export async function getUserChoice(text: string, values: R[], export function collectTerminalRegexps(grammar: Grammar): Record { const result: Record = {}; - const reachableRules = getAllReachableRules(grammar, false); + const reachableRules = GrammarUtils.getAllReachableRules(grammar, false); for (const terminalRule of stream(reachableRules).filter(GrammarAST.isTerminalRule)) { const name = terminalRule.name; - const regexp = terminalRegex(terminalRule); + const regexp = GrammarUtils.terminalRegex(terminalRule); result[name] = regexp; } return result; diff --git a/packages/langium-cli/src/parser-validation.ts b/packages/langium-cli/src/parser-validation.ts index 3d4977b73..3a41e7675 100644 --- a/packages/langium-cli/src/parser-validation.ts +++ b/packages/langium-cli/src/parser-validation.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { Grammar, IParserConfig, LangiumDocuments, LangiumParser, LanguageMetaData } from 'langium'; import type { LangiumConfig, LangiumLanguageConfig } from './package.js'; -import { getDocument, GrammarAST, prepareLangiumParser } from 'langium'; +import { AstUtils, GrammarAST, prepareLangiumParser } from 'langium'; import type { LangiumGrammarServices} from 'langium/grammar'; import { createServicesForGrammar } from 'langium/grammar'; import { getFilePath } from './package.js'; @@ -39,7 +39,7 @@ export async function validateParser(grammar: Grammar, config: LangiumConfig, gr if (defError.ruleName) { const rule = findRule(defError.ruleName, grammar, grammarServices.shared.workspace.LangiumDocuments); if (rule && rule.$cstNode) { - const filePath = getFilePath(getDocument(rule).uri.fsPath, config); + const filePath = getFilePath(AstUtils.getDocument(rule).uri.fsPath, config); const line = rule.$cstNode.range.start.line + 1; message += `${filePath}:${line} - `; } diff --git a/packages/langium-railroad/src/grammar-railroad.ts b/packages/langium-railroad/src/grammar-railroad.ts index f625f0813..c9239f5a1 100644 --- a/packages/langium-railroad/src/grammar-railroad.ts +++ b/packages/langium-railroad/src/grammar-railroad.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { GrammarAST, findNameAssignment } from 'langium'; +import { GrammarAST, GrammarUtils } from 'langium'; import { expandToStringLF, expandToStringLFWithNL } from 'langium/generate'; import type { FakeSVG } from 'railroad-diagrams'; import { default as rr } from 'railroad-diagrams'; @@ -137,7 +137,7 @@ function toRailroad(element: GrammarAST.AbstractElement): FakeSVG[] { } else if (GrammarAST.isRuleCall(element.terminal)) { return wrapCardinality(element.cardinality, new rr.NonTerminal(element.terminal.rule.$refText)); } else { - const nameAssignment = element.type.ref && findNameAssignment(element.type.ref); + const nameAssignment = element.type.ref && GrammarUtils.findNameAssignment(element.type.ref); if (nameAssignment) { return wrapCardinality(element.cardinality, toRailroad(nameAssignment)); } else { diff --git a/packages/langium-sprotty/src/diagnostic-marker-provider.ts b/packages/langium-sprotty/src/diagnostic-marker-provider.ts index 8f2152ca6..1e3bb14b0 100644 --- a/packages/langium-sprotty/src/diagnostic-marker-provider.ts +++ b/packages/langium-sprotty/src/diagnostic-marker-provider.ts @@ -9,7 +9,7 @@ import type { SModelElement } from 'sprotty-protocol'; import type { Diagnostic, Range } from 'vscode-languageserver'; import type { GeneratorContext } from './diagram-generator.js'; import type { LangiumSprottyServices } from './sprotty-services.js'; -import { getDocument } from 'langium'; +import { AstUtils } from 'langium'; /** * Used to add diagnostic markers to the generated diagram. @@ -56,7 +56,7 @@ export class DefaultDiagnosticMarkerProvider implements DiagnosticMarkerProvider } protected getDiagnostics(source: AstNode): Diagnostic[] { - const document = getDocument(source); + const document = AstUtils.getDocument(source); if (!document.diagnostics || !source.$cstNode) { return []; } diff --git a/packages/langium-sprotty/src/lsp.ts b/packages/langium-sprotty/src/lsp.ts index fea1cf083..e9b492344 100644 --- a/packages/langium-sprotty/src/lsp.ts +++ b/packages/langium-sprotty/src/lsp.ts @@ -8,7 +8,7 @@ import type { Connection } from 'vscode-languageserver'; import type { ActionMessage, DiagramState, SModelIndex } from 'sprotty-protocol'; import type { Location } from 'vscode-languageserver'; import type { LangiumSprottyServices, LangiumSprottySharedServices } from './sprotty-services.js'; -import { findDeclarationNodeAtOffset, getDocument, isOperationCancelled } from 'langium'; +import { AstUtils, CstUtils, isOperationCancelled } from 'langium'; import { findElement, FitToScreenAction, SelectAction, SelectAllAction } from 'sprotty-protocol'; import { NotificationType } from 'vscode-languageserver'; @@ -82,7 +82,7 @@ export function addDiagramSelectionHandler(services: LangiumSprottyServices): vo const nameNode = nameProvider.getNameNode(source); connection.sendNotification(OpenInTextEditorNotification.type, { location: { - uri: getDocument(source).uri.toString(), + uri: AstUtils.getDocument(source).uri.toString(), range: (nameNode ?? source.$cstNode).range }, forceOpen: false @@ -107,7 +107,7 @@ export function addTextSelectionHandler(services: LangiumSprottyServices, option if (diagramServers.length > 0) { const rootNode = document.parseResult.value.$cstNode; const offset = document.textDocument.offsetAt(position); - const selectedToken = findDeclarationNodeAtOffset(rootNode, offset, grammarConfig.nameRegexp); + const selectedToken = CstUtils.findDeclarationNodeAtOffset(rootNode, offset, grammarConfig.nameRegexp); const node = selectedToken?.astNode; if (node) { for (const diagramServer of diagramServers) { diff --git a/packages/langium-sprotty/src/trace-provider.ts b/packages/langium-sprotty/src/trace-provider.ts index 7b76c0be5..a68079b72 100644 --- a/packages/langium-sprotty/src/trace-provider.ts +++ b/packages/langium-sprotty/src/trace-provider.ts @@ -9,7 +9,7 @@ import type { SModelElement, SModelRoot } from 'sprotty-protocol'; import type { Range } from 'vscode-languageserver'; import { URI } from 'vscode-uri'; import type { LangiumSprottyServices } from './sprotty-services.js'; -import { findNodeForProperty, getDocument, stream, } from 'langium'; +import { AstUtils, GrammarUtils, stream, } from 'langium'; export interface TracedModelElement extends SModelElement { trace?: string @@ -51,7 +51,7 @@ export class DefaultTraceProvider implements TraceProvider { trace(target: TracedModelElement, source: AstNode, property?: string | undefined, index?: number | undefined): void { let range: Range | undefined; if (property) { - range = findNodeForProperty(source.$cstNode, property, index)?.range; + range = GrammarUtils.findNodeForProperty(source.$cstNode, property, index)?.range; } else { range = source.$cstNode?.range; } @@ -59,7 +59,7 @@ export class DefaultTraceProvider implements TraceProvider { return; } - const traceUri = getDocument(source).uri.with({ + const traceUri = AstUtils.getDocument(source).uri.with({ fragment: this.astNodeLocator.getAstNodePath(source), query: `${range.start.line}:${range.start.character}-${range.end.line}:${range.end.character}` }); @@ -84,7 +84,7 @@ export class DefaultTraceProvider implements TraceProvider { } getTarget(source: AstNode, root: SModelRoot): TracedModelElement | undefined { - const documentUri = getDocument(source).uri; + const documentUri = AstUtils.getDocument(source).uri; const containerChain = []; let currentContainer: AstNode | undefined = source; while (currentContainer) { diff --git a/packages/langium/src/dependency-injection.ts b/packages/langium/src/dependency-injection.ts index a235ce59b..b99b39702 100644 --- a/packages/langium/src/dependency-injection.ts +++ b/packages/langium/src/dependency-injection.ts @@ -43,8 +43,12 @@ export function inject(module1: Mod const isProxy = Symbol('isProxy'); -export function eagerLoad(item: any): any { - if (item && item[isProxy]) { +/** + * Eagerly load all services in the given dependency injection container. This is sometimes + * necessary because services can register event listeners in their constructors. + */ +export function eagerLoad(item: T): T { + if (item && (item as any)[isProxy]) { for (const value of Object.values(item)) { eagerLoad(value); } diff --git a/packages/langium/src/documentation/comment-provider.ts b/packages/langium/src/documentation/comment-provider.ts index f20f4543f..af0750e89 100644 --- a/packages/langium/src/documentation/comment-provider.ts +++ b/packages/langium/src/documentation/comment-provider.ts @@ -8,7 +8,7 @@ import type { GrammarConfig } from '../languages/grammar-config.js'; import { isAstNodeWithComment } from '../serializer/json-serializer.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import { findCommentNode } from '../utils/cst-util.js'; +import { findCommentNode } from '../utils/cst-utils.js'; /** * Provides comments for AST nodes. diff --git a/packages/langium/src/documentation/documentation-provider.ts b/packages/langium/src/documentation/documentation-provider.ts index 3c68c3f03..b44effe3e 100644 --- a/packages/langium/src/documentation/documentation-provider.ts +++ b/packages/langium/src/documentation/documentation-provider.ts @@ -9,7 +9,7 @@ import type { AstNode, AstNodeDescription } from '../syntax-tree.js'; import type { IndexManager } from '../workspace/index-manager.js'; import type { CommentProvider } from './comment-provider.js'; import type { JSDocTag } from './jsdoc.js'; -import { getDocument } from '../utils/ast-util.js'; +import { getDocument } from '../utils/ast-utils.js'; import { isJSDoc, parseJSDoc } from './jsdoc.js'; /** diff --git a/packages/langium/src/documentation/jsdoc.ts b/packages/langium/src/documentation/jsdoc.ts index 10938c4f5..32ae2202f 100644 --- a/packages/langium/src/documentation/jsdoc.ts +++ b/packages/langium/src/documentation/jsdoc.ts @@ -6,8 +6,8 @@ import type { CstNode } from '../syntax-tree.js'; import { Position, Range } from 'vscode-languageserver'; -import { NEWLINE_REGEXP, escapeRegExp } from '../utils/regex-util.js'; -import { URI } from '../utils/uri-util.js'; +import { NEWLINE_REGEXP, escapeRegExp } from '../utils/regexp-utils.js'; +import { URI } from '../utils/uri-utils.js'; export interface JSDocComment extends JSDocValue { readonly elements: JSDocElement[] diff --git a/packages/langium/src/generate/generator-tracing.ts b/packages/langium/src/generate/generator-tracing.ts index b4a8fa333..0f934a503 100644 --- a/packages/langium/src/generate/generator-tracing.ts +++ b/packages/langium/src/generate/generator-tracing.ts @@ -7,8 +7,8 @@ import type { Range } from 'vscode-languageserver-textdocument'; import type { AstNodeRegionWithAssignments, AstNodeWithTextRegion } from '../serializer/json-serializer.js'; import type { AstNode, CstNode, GenericAstNode } from '../syntax-tree.js'; -import { getDocument } from '../utils/ast-util.js'; -import { findNodesForProperty } from '../utils/grammar-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { findNodesForProperty } from '../utils/grammar-utils.js'; import { TreeStreamImpl } from '../utils/stream.js'; import type { DocumentSegment } from '../workspace/documents.js'; diff --git a/packages/langium/src/generate/template-node.ts b/packages/langium/src/generate/template-node.ts index 1cba2e1a0..4c64e6e09 100644 --- a/packages/langium/src/generate/template-node.ts +++ b/packages/langium/src/generate/template-node.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { AstNode, Properties } from '../syntax-tree.js'; -import { NEWLINE_REGEXP } from '../utils/regex-util.js'; +import { NEWLINE_REGEXP } from '../utils/regexp-utils.js'; import type { Generated, GeneratorNode, IndentNode } from './generator-node.js'; import { CompositeGeneratorNode, isGeneratorNode, traceToNode } from './generator-node.js'; import type { SourceRegion } from './generator-tracing.js'; diff --git a/packages/langium/src/generate/template-string.ts b/packages/langium/src/generate/template-string.ts index 593eeb802..11c00b5dd 100644 --- a/packages/langium/src/generate/template-string.ts +++ b/packages/langium/src/generate/template-string.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { NEWLINE_REGEXP } from '../utils/regex-util.js'; +import { NEWLINE_REGEXP } from '../utils/regexp-utils.js'; import { EOL, toString } from './generator-node.js'; export function expandToStringWithNL(staticParts: TemplateStringsArray, ...substitutions: unknown[]): string { diff --git a/packages/langium/src/grammar/internal-grammar-util.ts b/packages/langium/src/grammar/internal-grammar-util.ts index e893a8635..d27ed6a2c 100644 --- a/packages/langium/src/grammar/internal-grammar-util.ts +++ b/packages/langium/src/grammar/internal-grammar-util.ts @@ -4,12 +4,12 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { URI } from '../utils/uri-util.js'; +import { URI } from '../utils/uri-utils.js'; import type { LangiumDocuments } from '../workspace/documents.js'; import type { AstNode } from '../syntax-tree.js'; import * as ast from '../languages/generated/ast.js'; -import { getDocument } from '../utils/ast-util.js'; -import { UriUtils } from '../utils/uri-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { UriUtils } from '../utils/uri-utils.js'; import type { LangiumGrammarServices} from './langium-grammar-module.js'; import { createLangiumGrammarServices } from './langium-grammar-module.js'; import type { IParserConfig } from '../parser/parser-config.js'; @@ -20,7 +20,7 @@ import type { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumS import { EmptyFileSystem } from '../workspace/file-system-provider.js'; import { interpretAstReflection } from './ast-reflection-interpreter.js'; import { createDefaultModule, createDefaultSharedModule } from '../default-module.js'; -import { getTypeName, isDataType } from '../utils/grammar-util.js'; +import { getTypeName, isDataType } from '../utils/grammar-utils.js'; export function hasDataTypeReturn(rule: ast.ParserRule): boolean { const returnType = rule.returnType?.ref; diff --git a/packages/langium/src/grammar/langium-grammar-module.ts b/packages/langium/src/grammar/langium-grammar-module.ts index 54bce2017..c3dea89ab 100644 --- a/packages/langium/src/grammar/langium-grammar-module.ts +++ b/packages/langium/src/grammar/langium-grammar-module.ts @@ -26,7 +26,7 @@ import { LangiumGrammarDefinitionProvider } from './lsp/grammar-definition.js'; import { LangiumGrammarCallHierarchyProvider } from './lsp/grammar-call-hierarchy.js'; import { LangiumGrammarValidationResourcesCollector } from './validation/validation-resources-collector.js'; import { LangiumGrammarTypesValidator, registerTypeValidationChecks } from './validation/types-validator.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; import { DocumentState } from '../workspace/documents.js'; export type LangiumGrammarAddedServices = { diff --git a/packages/langium/src/grammar/lsp/grammar-call-hierarchy.ts b/packages/langium/src/grammar/lsp/grammar-call-hierarchy.ts index ab4b42383..141cd27e8 100644 --- a/packages/langium/src/grammar/lsp/grammar-call-hierarchy.ts +++ b/packages/langium/src/grammar/lsp/grammar-call-hierarchy.ts @@ -10,8 +10,8 @@ import type { Stream } from '../../utils/stream.js'; import type { ReferenceDescription } from '../../workspace/ast-descriptions.js'; import { SymbolKind } from 'vscode-languageserver'; import { AbstractCallHierarchyProvider } from '../../lsp/call-hierarchy-provider.js'; -import { getContainerOfType, getDocument, streamAllContents } from '../../utils/ast-util.js'; -import { findLeafNodeAtOffset } from '../../utils/cst-util.js'; +import { getContainerOfType, getDocument, streamAllContents } from '../../utils/ast-utils.js'; +import { findLeafNodeAtOffset } from '../../utils/cst-utils.js'; import { isParserRule, isRuleCall } from '../../languages/generated/ast.js'; export class LangiumGrammarCallHierarchyProvider extends AbstractCallHierarchyProvider { diff --git a/packages/langium/src/grammar/lsp/grammar-code-actions.ts b/packages/langium/src/grammar/lsp/grammar-code-actions.ts index e6118eabe..3bfbd86cb 100644 --- a/packages/langium/src/grammar/lsp/grammar-code-actions.ts +++ b/packages/langium/src/grammar/lsp/grammar-code-actions.ts @@ -7,21 +7,21 @@ import type { Diagnostic } from 'vscode-languageserver'; import type { CodeActionParams } from 'vscode-languageserver-protocol'; import type { CodeAction, Command, Position, TextEdit } from 'vscode-languageserver-types'; -import type { URI } from '../../utils/uri-util.js'; +import type { URI } from '../../utils/uri-utils.js'; import type { CodeActionProvider } from '../../lsp/code-action.js'; import type { LangiumServices } from '../../services.js'; import type { AstReflection, Reference, ReferenceInfo } from '../../syntax-tree.js'; -import type { MaybePromise } from '../../utils/promise-util.js'; +import type { MaybePromise } from '../../utils/promise-utils.js'; import type { LinkingErrorData } from '../../validation/document-validator.js'; import type { DiagnosticData } from '../../validation/validation-registry.js'; import type { LangiumDocument } from '../../workspace/documents.js'; import type { IndexManager } from '../../workspace/index-manager.js'; import { CodeActionKind } from 'vscode-languageserver'; -import { getContainerOfType } from '../../utils/ast-util.js'; -import { findLeafNodeAtOffset } from '../../utils/cst-util.js'; -import { findNodeForProperty } from '../../utils/grammar-util.js'; -import { escapeRegExp } from '../../utils/regex-util.js'; -import { UriUtils } from '../../utils/uri-util.js'; +import { getContainerOfType } from '../../utils/ast-utils.js'; +import { findLeafNodeAtOffset } from '../../utils/cst-utils.js'; +import { findNodeForProperty } from '../../utils/grammar-utils.js'; +import { escapeRegExp } from '../../utils/regexp-utils.js'; +import { UriUtils } from '../../utils/uri-utils.js'; import { DocumentValidator } from '../../validation/document-validator.js'; import * as ast from '../../languages/generated/ast.js'; import { IssueCodes } from '../validation/validator.js'; diff --git a/packages/langium/src/grammar/lsp/grammar-completion-provider.ts b/packages/langium/src/grammar/lsp/grammar-completion-provider.ts index 0dc512aba..362375b55 100644 --- a/packages/langium/src/grammar/lsp/grammar-completion-provider.ts +++ b/packages/langium/src/grammar/lsp/grammar-completion-provider.ts @@ -10,12 +10,12 @@ import type { NextFeature } from '../../lsp/completion/follow-element-computatio import type { CompletionAcceptor, CompletionContext } from '../../lsp/completion/completion-provider.js'; import { DefaultCompletionProvider } from '../../lsp/completion/completion-provider.js'; import type { LangiumServices } from '../../services.js'; -import type { MaybePromise } from '../../utils/promise-util.js'; -import { getContainerOfType } from '../../utils/ast-util.js'; +import type { MaybePromise } from '../../utils/promise-utils.js'; +import { getContainerOfType } from '../../utils/ast-utils.js'; import type { LangiumDocument, LangiumDocuments } from '../../workspace/documents.js'; import type { AbstractElement } from '../../languages/generated/ast.js'; import { isAssignment } from '../../languages/generated/ast.js'; -import { UriUtils } from '../../utils/uri-util.js'; +import { UriUtils } from '../../utils/uri-utils.js'; export class LangiumGrammarCompletionProvider extends DefaultCompletionProvider { diff --git a/packages/langium/src/grammar/lsp/grammar-definition.ts b/packages/langium/src/grammar/lsp/grammar-definition.ts index ff369120b..6c3333ebc 100644 --- a/packages/langium/src/grammar/lsp/grammar-definition.ts +++ b/packages/langium/src/grammar/lsp/grammar-definition.ts @@ -7,13 +7,13 @@ import type { DefinitionParams } from 'vscode-languageserver'; import type { LangiumServices } from '../../services.js'; import type { AstNode, LeafCstNode, Properties } from '../../syntax-tree.js'; -import type { MaybePromise } from '../../utils/promise-util.js'; +import type { MaybePromise } from '../../utils/promise-utils.js'; import type { LangiumDocuments } from '../../workspace/documents.js'; import type { Grammar, GrammarImport } from '../../languages/generated/ast.js'; import { LocationLink, Range } from 'vscode-languageserver'; import { DefaultDefinitionProvider } from '../../lsp/index.js'; -import { streamContents } from '../../utils/ast-util.js'; -import { findAssignment } from '../../utils/grammar-util.js'; +import { streamContents } from '../../utils/ast-utils.js'; +import { findAssignment } from '../../utils/grammar-utils.js'; import { isGrammarImport } from '../../languages/generated/ast.js'; import { resolveImport } from '../internal-grammar-util.js'; diff --git a/packages/langium/src/grammar/lsp/grammar-type-hierarchy.ts b/packages/langium/src/grammar/lsp/grammar-type-hierarchy.ts index 7c9570734..7eddd551c 100644 --- a/packages/langium/src/grammar/lsp/grammar-type-hierarchy.ts +++ b/packages/langium/src/grammar/lsp/grammar-type-hierarchy.ts @@ -7,8 +7,8 @@ import type { AstNode } from '../../syntax-tree.js'; import type { TypeHierarchyItem } from 'vscode-languageserver'; import { AbstractTypeHierarchyProvider } from '../../lsp/type-hierarchy-provider.js'; -import { getDocument } from '../../utils/ast-util.js'; -import { findLeafNodeAtOffset } from '../../utils/cst-util.js'; +import { getDocument } from '../../utils/ast-utils.js'; +import { findLeafNodeAtOffset } from '../../utils/cst-utils.js'; import { isInterface } from '../../languages/generated/ast.js'; export class LangiumGrammarTypeHierarchyProvider extends AbstractTypeHierarchyProvider { diff --git a/packages/langium/src/grammar/references/grammar-naming.ts b/packages/langium/src/grammar/references/grammar-naming.ts index 1710c9c0a..a517e3a31 100644 --- a/packages/langium/src/grammar/references/grammar-naming.ts +++ b/packages/langium/src/grammar/references/grammar-naming.ts @@ -6,7 +6,7 @@ import type { AstNode, CstNode } from '../../syntax-tree.js'; import { DefaultNameProvider } from '../../references/name-provider.js'; -import { findNodeForProperty } from '../../utils/grammar-util.js'; +import { findNodeForProperty } from '../../utils/grammar-utils.js'; import { isAssignment } from '../../languages/generated/ast.js'; export class LangiumGrammarNameProvider extends DefaultNameProvider { diff --git a/packages/langium/src/grammar/references/grammar-references.ts b/packages/langium/src/grammar/references/grammar-references.ts index e61c27575..06e3d54d7 100644 --- a/packages/langium/src/grammar/references/grammar-references.ts +++ b/packages/langium/src/grammar/references/grammar-references.ts @@ -12,11 +12,11 @@ import type { LangiumDocuments } from '../../workspace/documents.js'; import type { Action, Assignment, Interface, ParserRule, Type, TypeAttribute } from '../../languages/generated/ast.js'; import type { FindReferencesOptions } from '../../references/references.js'; import { DefaultReferences } from '../../references/references.js'; -import { getContainerOfType, getDocument } from '../../utils/ast-util.js'; -import { toDocumentSegment } from '../../utils/cst-util.js'; -import { findAssignment, findNodeForProperty, getActionAtElement } from '../../utils/grammar-util.js'; +import { getContainerOfType, getDocument } from '../../utils/ast-utils.js'; +import { toDocumentSegment } from '../../utils/cst-utils.js'; +import { findAssignment, findNodeForProperty, getActionAtElement } from '../../utils/grammar-utils.js'; import { stream } from '../../utils/stream.js'; -import { UriUtils } from '../../utils/uri-util.js'; +import { UriUtils } from '../../utils/uri-utils.js'; import { isAction, isAssignment, isInterface, isParserRule, isType, isTypeAttribute } from '../../languages/generated/ast.js'; import { extractAssignments } from '../internal-grammar-util.js'; import { collectChildrenTypes, collectSuperTypes } from '../type-system/types-util.js'; diff --git a/packages/langium/src/grammar/references/grammar-scope.ts b/packages/langium/src/grammar/references/grammar-scope.ts index 5e79e34f2..9cf11196b 100644 --- a/packages/langium/src/grammar/references/grammar-scope.ts +++ b/packages/langium/src/grammar/references/grammar-scope.ts @@ -14,12 +14,12 @@ import type { Grammar } from '../../languages/generated/ast.js'; import { EMPTY_SCOPE, MapScope } from '../../references/scope.js'; import { DefaultScopeComputation } from '../../references/scope-computation.js'; import { DefaultScopeProvider } from '../../references/scope-provider.js'; -import { findRootNode, getContainerOfType, getDocument, streamAllContents } from '../../utils/ast-util.js'; -import { toDocumentSegment } from '../../utils/cst-util.js'; +import { findRootNode, getContainerOfType, getDocument, streamAllContents } from '../../utils/ast-utils.js'; +import { toDocumentSegment } from '../../utils/cst-utils.js'; import { stream } from '../../utils/stream.js'; import { AbstractType, Interface, isAction, isGrammar, isParserRule, isReturnType, Type } from '../../languages/generated/ast.js'; import { resolveImportUri } from '../internal-grammar-util.js'; -import { getActionType } from '../../utils/grammar-util.js'; +import { getActionType } from '../../utils/grammar-utils.js'; export class LangiumGrammarScopeProvider extends DefaultScopeProvider { diff --git a/packages/langium/src/grammar/type-system/type-collector/all-types.ts b/packages/langium/src/grammar/type-system/type-collector/all-types.ts index 8ed444d75..31419a103 100644 --- a/packages/langium/src/grammar/type-system/type-collector/all-types.ts +++ b/packages/langium/src/grammar/type-system/type-collector/all-types.ts @@ -5,16 +5,16 @@ ******************************************************************************/ import type { ParserRule, Interface, Type, Grammar } from '../../../languages/generated/ast.js'; -import type { URI } from '../../../utils/uri-util.js'; +import type { URI } from '../../../utils/uri-utils.js'; import type { LangiumDocuments } from '../../../workspace/documents.js'; import type { PlainAstTypes } from './plain-types.js'; import type { AstTypes } from './types.js'; import { collectInferredTypes } from './inferred-types.js'; import { collectDeclaredTypes } from './declared-types.js'; -import { getDocument } from '../../../utils/ast-util.js'; +import { getDocument } from '../../../utils/ast-utils.js'; import { isParserRule } from '../../../languages/generated/ast.js'; import { resolveImport } from '../../internal-grammar-util.js'; -import { isDataTypeRule } from '../../../utils/grammar-util.js'; +import { isDataTypeRule } from '../../../utils/grammar-utils.js'; export type AstResources = { parserRules: ParserRule[], diff --git a/packages/langium/src/grammar/type-system/type-collector/declared-types.ts b/packages/langium/src/grammar/type-system/type-collector/declared-types.ts index e1d8e0aa3..a0368a331 100644 --- a/packages/langium/src/grammar/type-system/type-collector/declared-types.ts +++ b/packages/langium/src/grammar/type-system/type-collector/declared-types.ts @@ -9,7 +9,7 @@ import { isArrayLiteral, isBooleanLiteral } from '../../../languages/generated/a import type { PlainAstTypes, PlainInterface, PlainProperty, PlainPropertyDefaultValue, PlainPropertyType, PlainUnion } from './plain-types.js'; import { isArrayType, isReferenceType, isUnionType, isSimpleType } from '../../../languages/generated/ast.js'; import { getTypeNameWithoutError, isPrimitiveGrammarType } from '../../internal-grammar-util.js'; -import { getTypeName } from '../../../utils/grammar-util.js'; +import { getTypeName } from '../../../utils/grammar-utils.js'; export function collectDeclaredTypes(interfaces: Interface[], unions: Type[]): PlainAstTypes { const declaredTypes: PlainAstTypes = { unions: [], interfaces: [] }; diff --git a/packages/langium/src/grammar/type-system/type-collector/inferred-types.ts b/packages/langium/src/grammar/type-system/type-collector/inferred-types.ts index 36bd70260..1f02a27fb 100644 --- a/packages/langium/src/grammar/type-system/type-collector/inferred-types.ts +++ b/packages/langium/src/grammar/type-system/type-collector/inferred-types.ts @@ -11,7 +11,7 @@ import { MultiMap } from '../../../utils/collections.js'; import { isAlternatives, isKeyword, isParserRule, isAction, isGroup, isUnorderedGroup, isAssignment, isRuleCall, isCrossReference, isTerminalRule } from '../../../languages/generated/ast.js'; import { getTypeNameWithoutError, isPrimitiveGrammarType } from '../../internal-grammar-util.js'; import { mergePropertyTypes } from './plain-types.js'; -import { isOptionalCardinality, terminalRegex, getRuleType } from '../../../utils/grammar-util.js'; +import { isOptionalCardinality, terminalRegex, getRuleType } from '../../../utils/grammar-utils.js'; interface TypePart { name?: string diff --git a/packages/langium/src/grammar/validation/validation-resources-collector.ts b/packages/langium/src/grammar/validation/validation-resources-collector.ts index bb26ac6ab..13e4315c8 100644 --- a/packages/langium/src/grammar/validation/validation-resources-collector.ts +++ b/packages/langium/src/grammar/validation/validation-resources-collector.ts @@ -15,7 +15,7 @@ import { stream } from '../../utils/stream.js'; import { isAction, isAlternatives, isGroup, isUnorderedGroup } from '../../languages/generated/ast.js'; import { mergeInterfaces, mergeTypesAndInterfaces } from '../type-system/types-util.js'; import { collectValidationAst } from '../type-system/ast-collector.js'; -import { getActionType, getRuleType } from '../../utils/grammar-util.js'; +import { getActionType, getRuleType } from '../../utils/grammar-utils.js'; export class LangiumGrammarValidationResourcesCollector { private readonly documents: LangiumDocuments; diff --git a/packages/langium/src/grammar/validation/validator.ts b/packages/langium/src/grammar/validation/validator.ts index 1610b5216..98ae606c0 100644 --- a/packages/langium/src/grammar/validation/validator.ts +++ b/packages/langium/src/grammar/validation/validator.ts @@ -13,10 +13,10 @@ import type { LangiumDocuments } from '../../workspace/documents.js'; import type { LangiumGrammarServices } from '../langium-grammar-module.js'; import type { Range } from 'vscode-languageserver-types'; import { DiagnosticTag } from 'vscode-languageserver-types'; -import { getContainerOfType, streamAllContents } from '../../utils/ast-util.js'; +import { getContainerOfType, streamAllContents } from '../../utils/ast-utils.js'; import { MultiMap } from '../../utils/collections.js'; -import { toDocumentSegment } from '../../utils/cst-util.js'; -import { findNameAssignment, findNodeForKeyword, findNodeForProperty, getAllReachableRules, isDataTypeRule, isOptionalCardinality, terminalRegex } from '../../utils/grammar-util.js'; +import { toDocumentSegment } from '../../utils/cst-utils.js'; +import { findNameAssignment, findNodeForKeyword, findNodeForProperty, getAllReachableRules, isDataTypeRule, isOptionalCardinality, terminalRegex } from '../../utils/grammar-utils.js'; import { stream } from '../../utils/stream.js'; import { diagnosticData } from '../../validation/validation-registry.js'; import * as ast from '../../languages/generated/ast.js'; diff --git a/packages/langium/src/languages/grammar-config.ts b/packages/langium/src/languages/grammar-config.ts index acb86bd33..4dcd160b2 100644 --- a/packages/langium/src/languages/grammar-config.ts +++ b/packages/langium/src/languages/grammar-config.ts @@ -5,11 +5,11 @@ ******************************************************************************/ import type { LangiumServices } from '../services.js'; -import { DefaultNameRegexp } from '../utils/cst-util.js'; -import { isCommentTerminal } from '../utils/grammar-util.js'; -import { isMultilineComment } from '../utils/regex-util.js'; +import { DefaultNameRegexp } from '../utils/cst-utils.js'; +import { isCommentTerminal } from '../utils/grammar-utils.js'; +import { isMultilineComment } from '../utils/regexp-utils.js'; import { isTerminalRule } from './generated/ast.js'; -import { terminalRegex } from '../utils/grammar-util.js'; +import { terminalRegex } from '../utils/grammar-utils.js'; export interface GrammarConfig { /** @@ -22,6 +22,10 @@ export interface GrammarConfig { nameRegexp: RegExp } +/** + * Create the default grammar configuration (used by `createDefaultModule`). This can be overridden in a + * language-specific module. + */ export function createGrammarConfig(services: LangiumServices): GrammarConfig { const rules: string[] = []; const grammar = services.Grammar; diff --git a/packages/langium/src/lsp/call-hierarchy-provider.ts b/packages/langium/src/lsp/call-hierarchy-provider.ts index 7349abdce..bde3e454a 100644 --- a/packages/langium/src/lsp/call-hierarchy-provider.ts +++ b/packages/langium/src/lsp/call-hierarchy-provider.ts @@ -13,10 +13,10 @@ import type { AstNode } from '../syntax-tree.js'; import type { Stream } from '../utils/stream.js'; import type { ReferenceDescription } from '../workspace/ast-descriptions.js'; import type { LangiumDocument, LangiumDocuments } from '../workspace/documents.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import { SymbolKind } from 'vscode-languageserver'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; -import { URI } from '../utils/uri-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; +import { URI } from '../utils/uri-utils.js'; /** * Language-specific service for handling call hierarchy requests. diff --git a/packages/langium/src/lsp/code-action.ts b/packages/langium/src/lsp/code-action.ts index 0163afb06..536307b79 100644 --- a/packages/langium/src/lsp/code-action.ts +++ b/packages/langium/src/lsp/code-action.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { CancellationToken, CodeAction, CodeActionParams, Command } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; export interface CodeActionProvider { diff --git a/packages/langium/src/lsp/code-lens-provider.ts b/packages/langium/src/lsp/code-lens-provider.ts index 9ace50d8c..9b0e99351 100644 --- a/packages/langium/src/lsp/code-lens-provider.ts +++ b/packages/langium/src/lsp/code-lens-provider.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { CancellationToken, CodeLens, CodeLensParams } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; export interface CodeLensProvider { diff --git a/packages/langium/src/lsp/completion/completion-provider.ts b/packages/langium/src/lsp/completion/completion-provider.ts index 90e5b19f7..8d615ca32 100644 --- a/packages/langium/src/lsp/completion/completion-provider.ts +++ b/packages/langium/src/lsp/completion/completion-provider.ts @@ -11,7 +11,7 @@ import type { NameProvider } from '../../references/name-provider.js'; import type { ScopeProvider } from '../../references/scope-provider.js'; import type { LangiumServices } from '../../services.js'; import type { AstNode, AstNodeDescription, AstReflection, CstNode, ReferenceInfo } from '../../syntax-tree.js'; -import type { MaybePromise } from '../../utils/promise-util.js'; +import type { MaybePromise } from '../../utils/promise-utils.js'; import type { LangiumDocument } from '../../workspace/documents.js'; import type { NextFeature } from './follow-element-computation.js'; import type { NodeKindProvider } from '../node-kind-provider.js'; @@ -21,9 +21,9 @@ import type { Lexer } from '../../parser/lexer.js'; import type { IToken } from 'chevrotain'; import { CompletionItemKind, CompletionList, Position } from 'vscode-languageserver'; import * as ast from '../../languages/generated/ast.js'; -import { assignMandatoryAstProperties, getContainerOfType } from '../../utils/ast-util.js'; -import { findDeclarationNodeAtOffset, findLeafNodeBeforeOffset } from '../../utils/cst-util.js'; -import { getEntryRule, getExplicitRuleType } from '../../utils/grammar-util.js'; +import { assignMandatoryProperties, getContainerOfType } from '../../utils/ast-utils.js'; +import { findDeclarationNodeAtOffset, findLeafNodeBeforeOffset } from '../../utils/cst-utils.js'; +import { getEntryRule, getExplicitRuleType } from '../../utils/grammar-utils.js'; import { stream } from '../../utils/stream.js'; import { findFirstFeatures, findNextFeatures } from './follow-element-computation.js'; @@ -418,7 +418,7 @@ export class DefaultCompletionProvider implements CompletionProvider { $container: node, $containerProperty: next.property }; - assignMandatoryAstProperties(this.astReflection, node); + assignMandatoryProperties(this.astReflection, node); } const refInfo: ReferenceInfo = { reference: { diff --git a/packages/langium/src/lsp/completion/follow-element-computation.ts b/packages/langium/src/lsp/completion/follow-element-computation.ts index 32f8a454d..f34bbc1ea 100644 --- a/packages/langium/src/lsp/completion/follow-element-computation.ts +++ b/packages/langium/src/lsp/completion/follow-element-computation.ts @@ -7,9 +7,9 @@ import type { IToken } from 'chevrotain'; import * as ast from '../../languages/generated/ast.js'; import { isAstNode } from '../../syntax-tree.js'; -import { getContainerOfType } from '../../utils/ast-util.js'; -import type { Cardinality } from '../../utils/grammar-util.js'; -import { getCrossReferenceTerminal, getExplicitRuleType, getTypeName, isArrayCardinality, isOptionalCardinality, terminalRegex } from '../../utils/grammar-util.js'; +import { getContainerOfType } from '../../utils/ast-utils.js'; +import type { Cardinality } from '../../utils/grammar-utils.js'; +import { getCrossReferenceTerminal, getExplicitRuleType, getTypeName, isArrayCardinality, isOptionalCardinality, terminalRegex } from '../../utils/grammar-utils.js'; export interface NextFeature { /** diff --git a/packages/langium/src/lsp/declaration-provider.ts b/packages/langium/src/lsp/declaration-provider.ts index 3fc3297bc..a92e905b0 100644 --- a/packages/langium/src/lsp/declaration-provider.ts +++ b/packages/langium/src/lsp/declaration-provider.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { CancellationToken, DeclarationParams, LocationLink } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; /** diff --git a/packages/langium/src/lsp/definition-provider.ts b/packages/langium/src/lsp/definition-provider.ts index 68302e828..9b309ed76 100644 --- a/packages/langium/src/lsp/definition-provider.ts +++ b/packages/langium/src/lsp/definition-provider.ts @@ -10,11 +10,11 @@ import type { NameProvider } from '../references/name-provider.js'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { CstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { LocationLink } from 'vscode-languageserver'; -import { getDocument } from '../utils/ast-util.js'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling go to definition requests. diff --git a/packages/langium/src/lsp/document-highlight-provider.ts b/packages/langium/src/lsp/document-highlight-provider.ts index bf3300ead..8275faded 100644 --- a/packages/langium/src/lsp/document-highlight-provider.ts +++ b/packages/langium/src/lsp/document-highlight-provider.ts @@ -9,13 +9,13 @@ import type { GrammarConfig } from '../languages/grammar-config.js'; import type { NameProvider } from '../references/name-provider.js'; import type { FindReferencesOptions, References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { ReferenceDescription } from '../workspace/ast-descriptions.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { DocumentHighlight } from 'vscode-languageserver'; -import { getDocument } from '../utils/ast-util.js'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; -import { UriUtils } from '../utils/uri-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; +import { UriUtils } from '../utils/uri-utils.js'; /** * Language-specific service for handling document highlight requests. diff --git a/packages/langium/src/lsp/document-link-provider.ts b/packages/langium/src/lsp/document-link-provider.ts index fae049728..a15455045 100644 --- a/packages/langium/src/lsp/document-link-provider.ts +++ b/packages/langium/src/lsp/document-link-provider.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { CancellationToken, DocumentLink, DocumentLinkParams } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; /** diff --git a/packages/langium/src/lsp/document-symbol-provider.ts b/packages/langium/src/lsp/document-symbol-provider.ts index 807825e15..72897ec5d 100644 --- a/packages/langium/src/lsp/document-symbol-provider.ts +++ b/packages/langium/src/lsp/document-symbol-provider.ts @@ -8,8 +8,8 @@ import type { CancellationToken, DocumentSymbol, DocumentSymbolParams} from 'vsc import type { NameProvider } from '../references/name-provider.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import { streamContents } from '../utils/ast-util.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import { streamContents } from '../utils/ast-utils.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import type { NodeKindProvider } from './node-kind-provider.js'; diff --git a/packages/langium/src/lsp/document-update-handler.ts b/packages/langium/src/lsp/document-update-handler.ts index 3952f7b61..ed1dd153b 100644 --- a/packages/langium/src/lsp/document-update-handler.ts +++ b/packages/langium/src/lsp/document-update-handler.ts @@ -10,7 +10,7 @@ import type { LangiumSharedServices } from '../services.js'; import type { WorkspaceLock } from '../workspace/workspace-lock.js'; import type { DocumentBuilder } from '../workspace/document-builder.js'; import { DidChangeWatchedFilesNotification, FileChangeType } from 'vscode-languageserver'; -import { URI } from '../utils/uri-util.js'; +import { URI } from '../utils/uri-utils.js'; import { stream } from '../utils/stream.js'; /** diff --git a/packages/langium/src/lsp/execute-command-handler.ts b/packages/langium/src/lsp/execute-command-handler.ts index c3f787ed5..183f7b820 100644 --- a/packages/langium/src/lsp/execute-command-handler.ts +++ b/packages/langium/src/lsp/execute-command-handler.ts @@ -6,7 +6,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import { CancellationToken } from 'vscode-languageserver'; export interface ExecuteCommandHandler { diff --git a/packages/langium/src/lsp/file-operation-handler.ts b/packages/langium/src/lsp/file-operation-handler.ts index 2b53b93eb..4de16d99e 100644 --- a/packages/langium/src/lsp/file-operation-handler.ts +++ b/packages/langium/src/lsp/file-operation-handler.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { CreateFilesParams, DeleteFilesParams, FileOperationOptions, RenameFilesParams, WorkspaceEdit } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; /** * Shared service for handling file changes such as file creation, deletion and renaming. diff --git a/packages/langium/src/lsp/folding-range-provider.ts b/packages/langium/src/lsp/folding-range-provider.ts index 726805522..fd2f35a5c 100644 --- a/packages/langium/src/lsp/folding-range-provider.ts +++ b/packages/langium/src/lsp/folding-range-provider.ts @@ -7,11 +7,11 @@ import type { CancellationToken, FoldingRangeParams } from 'vscode-languageserver'; import type { LangiumServices } from '../services.js'; import type { AstNode, CstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { FoldingRange, FoldingRangeKind } from 'vscode-languageserver'; -import { streamAllContents } from '../utils/ast-util.js'; -import { flattenCst } from '../utils/cst-util.js'; +import { streamAllContents } from '../utils/ast-utils.js'; +import { flattenCst } from '../utils/cst-utils.js'; /** * Language-specific service for handling folding range requests. diff --git a/packages/langium/src/lsp/formatter.ts b/packages/langium/src/lsp/formatter.ts index bea2b6075..c90d87778 100644 --- a/packages/langium/src/lsp/formatter.ts +++ b/packages/langium/src/lsp/formatter.ts @@ -7,13 +7,13 @@ import type { CancellationToken, DocumentFormattingParams, DocumentOnTypeFormattingOptions, DocumentOnTypeFormattingParams, DocumentRangeFormattingParams, FormattingOptions, Range, TextEdit } from 'vscode-languageserver'; import type { TextDocument } from 'vscode-languageserver-textdocument'; import type { AstNode, CstNode, Properties } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { Stream } from '../utils/stream.js'; import type { LangiumDocument } from '../workspace/documents.js'; -import { findNodeForKeyword, findNodesForKeyword, findNodeForProperty, findNodesForProperty } from '../utils/grammar-util.js'; +import { findNodeForKeyword, findNodesForKeyword, findNodeForProperty, findNodesForProperty } from '../utils/grammar-utils.js'; import { isCompositeCstNode, isLeafCstNode } from '../syntax-tree.js'; -import { streamAllContents } from '../utils/ast-util.js'; -import { getInteriorNodes, getNextNode } from '../utils/cst-util.js'; +import { streamAllContents } from '../utils/ast-utils.js'; +import { getInteriorNodes, getNextNode } from '../utils/cst-utils.js'; import { DONE_RESULT, EMPTY_STREAM, StreamImpl, TreeStreamImpl } from '../utils/stream.js'; /** diff --git a/packages/langium/src/lsp/hover-provider.ts b/packages/langium/src/lsp/hover-provider.ts index 385ff0711..e7a2b6edd 100644 --- a/packages/langium/src/lsp/hover-provider.ts +++ b/packages/langium/src/lsp/hover-provider.ts @@ -9,10 +9,10 @@ import type { GrammarConfig } from '../languages/grammar-config.js'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import type { DocumentationProvider } from '../documentation/documentation-provider.js'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling hover requests. diff --git a/packages/langium/src/lsp/implementation-provider.ts b/packages/langium/src/lsp/implementation-provider.ts index fab684bc8..89fd82eb4 100644 --- a/packages/langium/src/lsp/implementation-provider.ts +++ b/packages/langium/src/lsp/implementation-provider.ts @@ -9,10 +9,10 @@ import type { GrammarConfig } from '../languages/grammar-config.js'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { CancellationToken } from 'vscode-languageserver'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling go to implementation requests. diff --git a/packages/langium/src/lsp/inlay-hint-provider.ts b/packages/langium/src/lsp/inlay-hint-provider.ts index 82d1c35c9..0f6a0be09 100644 --- a/packages/langium/src/lsp/inlay-hint-provider.ts +++ b/packages/langium/src/lsp/inlay-hint-provider.ts @@ -7,10 +7,10 @@ import type { InlayHint, InlayHintParams } from 'vscode-languageserver'; import type { AstNode } from '../syntax-tree.js'; import { CancellationToken } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; -import { streamAst } from '../utils/ast-util.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { streamAst } from '../utils/ast-utils.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; export type InlayHintAcceptor = (inlayHint: InlayHint) => void; diff --git a/packages/langium/src/lsp/language-server.ts b/packages/langium/src/lsp/language-server.ts index 0a057eb8c..9c9e4efdc 100644 --- a/packages/langium/src/lsp/language-server.ts +++ b/packages/langium/src/lsp/language-server.ts @@ -32,12 +32,12 @@ import type { LangiumServices, LangiumSharedServices } from '../services.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { Emitter, LSPErrorCodes, ResponseError, TextDocumentSyncKind } from 'vscode-languageserver'; import { eagerLoad } from '../dependency-injection.js'; -import { isOperationCancelled } from '../utils/promise-util.js'; +import { isOperationCancelled } from '../utils/promise-utils.js'; import { DocumentState } from '../workspace/documents.js'; import { mergeCompletionProviderOptions } from './completion/completion-provider.js'; import { DefaultSemanticTokenOptions } from './semantic-token-provider.js'; import { mergeSignatureHelpOptions } from './signature-help-provider.js'; -import { URI } from '../utils/uri-util.js'; +import { URI } from '../utils/uri-utils.js'; export interface LanguageServer { initialize(params: InitializeParams): Promise diff --git a/packages/langium/src/lsp/references-provider.ts b/packages/langium/src/lsp/references-provider.ts index 5151e7b19..f23d1cbbb 100644 --- a/packages/langium/src/lsp/references-provider.ts +++ b/packages/langium/src/lsp/references-provider.ts @@ -9,11 +9,11 @@ import type { NameProvider } from '../references/name-provider.js'; import type { References } from '../references/references.js'; import type { LeafCstNode } from '../syntax-tree.js'; import type { LangiumServices } from '../services.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import type { GrammarConfig } from '../languages/grammar-config.js'; import { Location } from 'vscode-languageserver'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling find references requests. diff --git a/packages/langium/src/lsp/rename-provider.ts b/packages/langium/src/lsp/rename-provider.ts index 9b1cff079..848989744 100644 --- a/packages/langium/src/lsp/rename-provider.ts +++ b/packages/langium/src/lsp/rename-provider.ts @@ -10,11 +10,11 @@ import type { NameProvider } from '../references/name-provider.js'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { CstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { TextEdit } from 'vscode-languageserver'; import { isNamed } from '../references/name-provider.js'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling rename requests and prepare rename requests. diff --git a/packages/langium/src/lsp/semantic-token-provider.ts b/packages/langium/src/lsp/semantic-token-provider.ts index b4f276185..f00afa963 100644 --- a/packages/langium/src/lsp/semantic-token-provider.ts +++ b/packages/langium/src/lsp/semantic-token-provider.ts @@ -10,12 +10,12 @@ import type { Range, SemanticTokens, SemanticTokensClientCapabilities, SemanticT import type { LangiumServices } from '../services.js'; import type { AstNode, CstNode, Properties } from '../syntax-tree.js'; import type { LangiumDocument } from '../workspace/documents.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import { CancellationToken, SemanticTokenModifiers, SemanticTokensBuilder as BaseSemanticTokensBuilder, SemanticTokenTypes } from 'vscode-languageserver'; -import { findNodesForKeyword, findNodeForProperty, findNodesForProperty, findNodeForKeyword } from '../utils/grammar-util.js'; -import { streamAst } from '../utils/ast-util.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; -import { inRange } from '../utils/cst-util.js'; +import { findNodesForKeyword, findNodeForProperty, findNodesForProperty, findNodeForKeyword } from '../utils/grammar-utils.js'; +import { streamAst } from '../utils/ast-utils.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; +import { inRange } from '../utils/cst-utils.js'; export const AllSemanticTokenTypes: Record = { [SemanticTokenTypes.class]: 0, diff --git a/packages/langium/src/lsp/signature-help-provider.ts b/packages/langium/src/lsp/signature-help-provider.ts index 6367c7123..37ac7ce43 100644 --- a/packages/langium/src/lsp/signature-help-provider.ts +++ b/packages/langium/src/lsp/signature-help-provider.ts @@ -7,8 +7,8 @@ import type { SignatureHelp, SignatureHelpOptions, SignatureHelpParams } from 'vscode-languageserver'; import { CancellationToken } from 'vscode-languageserver'; import type { AstNode } from '../syntax-tree.js'; -import { findLeafNodeAtOffset } from '../utils/cst-util.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import { findLeafNodeAtOffset } from '../utils/cst-utils.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; /** diff --git a/packages/langium/src/lsp/type-hierarchy-provider.ts b/packages/langium/src/lsp/type-hierarchy-provider.ts index 94dcafe26..431210e07 100644 --- a/packages/langium/src/lsp/type-hierarchy-provider.ts +++ b/packages/langium/src/lsp/type-hierarchy-provider.ts @@ -17,10 +17,10 @@ import type { NameProvider } from '../references/name-provider.js'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; -import { URI } from '../utils/uri-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; +import { URI } from '../utils/uri-utils.js'; import type { LangiumDocument, LangiumDocuments } from '../workspace/documents.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; /** * Language-specific service for handling type hierarchy requests. diff --git a/packages/langium/src/lsp/type-provider.ts b/packages/langium/src/lsp/type-provider.ts index 2a0b9976d..08a543e2b 100644 --- a/packages/langium/src/lsp/type-provider.ts +++ b/packages/langium/src/lsp/type-provider.ts @@ -8,10 +8,10 @@ import type { LocationLink, TypeDefinitionParams } from 'vscode-languageserver'; import type { References } from '../references/references.js'; import type { LangiumServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { CancellationToken } from 'vscode-languageserver'; -import { findDeclarationNodeAtOffset } from '../utils/cst-util.js'; +import { findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; /** * Language-specific service for handling go to type requests. diff --git a/packages/langium/src/lsp/workspace-symbol-provider.ts b/packages/langium/src/lsp/workspace-symbol-provider.ts index 53d89b167..1a6d50ed0 100644 --- a/packages/langium/src/lsp/workspace-symbol-provider.ts +++ b/packages/langium/src/lsp/workspace-symbol-provider.ts @@ -7,12 +7,12 @@ import type { WorkspaceSymbol, WorkspaceSymbolParams } from 'vscode-languageserver'; import type { LangiumSharedServices } from '../services.js'; import type { IndexManager } from '../workspace/index-manager.js'; -import type { MaybePromise} from '../utils/promise-util.js'; +import type { MaybePromise} from '../utils/promise-utils.js'; import type { AstNodeDescription } from '../syntax-tree.js'; import type { NodeKindProvider } from './node-kind-provider.js'; import type { FuzzyMatcher } from './fuzzy-matcher.js'; import { CancellationToken } from 'vscode-languageserver'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; /** * Shared service for handling workspace symbols requests. diff --git a/packages/langium/src/node/node-file-system-provider.ts b/packages/langium/src/node/node-file-system-provider.ts index 6d3c29f95..740929a23 100644 --- a/packages/langium/src/node/node-file-system-provider.ts +++ b/packages/langium/src/node/node-file-system-provider.ts @@ -4,8 +4,8 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { URI } from '../utils/uri-util.js'; -import { UriUtils } from '../utils/uri-util.js'; +import type { URI } from '../utils/uri-utils.js'; +import { UriUtils } from '../utils/uri-utils.js'; import type { FileSystemNode, FileSystemProvider } from '../workspace/file-system-provider.js'; import * as fs from 'node:fs'; diff --git a/packages/langium/src/parser/cst-node-builder.ts b/packages/langium/src/parser/cst-node-builder.ts index a8a7a6e08..00b8a9e8f 100644 --- a/packages/langium/src/parser/cst-node-builder.ts +++ b/packages/langium/src/parser/cst-node-builder.ts @@ -10,7 +10,7 @@ import type { AbstractElement } from '../languages/generated/ast.js'; import type { AstNode, CompositeCstNode, CstNode, LeafCstNode, RootCstNode } from '../syntax-tree.js'; import { Position } from 'vscode-languageserver-types'; import { isCompositeCstNode } from '../syntax-tree.js'; -import { tokenToRange } from '../utils/cst-util.js'; +import { tokenToRange } from '../utils/cst-utils.js'; export class CstNodeBuilder { diff --git a/packages/langium/src/parser/langium-parser.ts b/packages/langium/src/parser/langium-parser.ts index 6565f4f5e..9ddc7557c 100644 --- a/packages/langium/src/parser/langium-parser.ts +++ b/packages/langium/src/parser/langium-parser.ts @@ -16,8 +16,8 @@ import type { ValueConverter } from './value-converter.js'; import { defaultParserErrorProvider, EmbeddedActionsParser, LLkLookaheadStrategy } from 'chevrotain'; import { LLStarLookaheadStrategy } from 'chevrotain-allstar'; import { isAssignment, isCrossReference, isKeyword } from '../languages/generated/ast.js'; -import { getTypeName, isDataTypeRule } from '../utils/grammar-util.js'; -import { assignMandatoryAstProperties, getContainerOfType, linkContentToContainer } from '../utils/ast-util.js'; +import { getTypeName, isDataTypeRule } from '../utils/grammar-utils.js'; +import { assignMandatoryProperties, getContainerOfType, linkContentToContainer } from '../utils/ast-utils.js'; import { CstNodeBuilder } from './cst-node-builder.js'; export type ParseResult = { @@ -285,7 +285,7 @@ export class LangiumParser extends AbstractLangiumParser { if (isDataTypeNode(obj)) { return this.converter.convert(obj.value, obj.$cstNode); } else { - assignMandatoryAstProperties(this.astReflection, obj); + assignMandatoryProperties(this.astReflection, obj); } return obj; } diff --git a/packages/langium/src/parser/parser-builder-base.ts b/packages/langium/src/parser/parser-builder-base.ts index e13d8fe6c..ae667364b 100644 --- a/packages/langium/src/parser/parser-builder-base.ts +++ b/packages/langium/src/parser/parser-builder-base.ts @@ -8,12 +8,12 @@ import type { IOrAlt, TokenType, TokenTypeDictionary } from 'chevrotain'; import type { AbstractElement, Action, Alternatives, Condition, CrossReference, Grammar, Group, Keyword, NamedArgument, ParserRule, RuleCall, UnorderedGroup } from '../languages/generated/ast.js'; import type { BaseParser } from './langium-parser.js'; import type { AstNode } from '../syntax-tree.js'; -import type { Cardinality } from '../utils/grammar-util.js'; +import type { Cardinality } from '../utils/grammar-utils.js'; import { EMPTY_ALT, EOF } from 'chevrotain'; import { isAction, isAlternatives, isEndOfFile, isAssignment, isConjunction, isCrossReference, isDisjunction, isGroup, isKeyword, isNegation, isParameterReference, isParserRule, isRuleCall, isTerminalRule, isUnorderedGroup, isBooleanLiteral } from '../languages/generated/ast.js'; import { assertUnreachable, ErrorWithLocation } from '../utils/errors.js'; import { stream } from '../utils/stream.js'; -import { findNameAssignment, getAllReachableRules, getTypeName } from '../utils/grammar-util.js'; +import { findNameAssignment, getAllReachableRules, getTypeName } from '../utils/grammar-utils.js'; type RuleContext = { optional: number, diff --git a/packages/langium/src/parser/token-builder.ts b/packages/langium/src/parser/token-builder.ts index f7a5ccd8f..29a4a6452 100644 --- a/packages/langium/src/parser/token-builder.ts +++ b/packages/langium/src/parser/token-builder.ts @@ -9,9 +9,9 @@ import type { AbstractRule, Grammar, Keyword, TerminalRule } from '../languages/ import type { Stream } from '../utils/stream.js'; import { Lexer } from 'chevrotain'; import { isKeyword, isParserRule, isTerminalRule } from '../languages/generated/ast.js'; -import { streamAllContents } from '../utils/ast-util.js'; -import { getAllReachableRules, terminalRegex } from '../utils/grammar-util.js'; -import { getCaseInsensitivePattern, isWhitespaceRegExp, partialMatches } from '../utils/regex-util.js'; +import { streamAllContents } from '../utils/ast-utils.js'; +import { getAllReachableRules, terminalRegex } from '../utils/grammar-utils.js'; +import { getCaseInsensitivePattern, isWhitespace, partialMatches } from '../utils/regexp-utils.js'; import { stream } from '../utils/stream.js'; export interface TokenBuilderOptions { @@ -31,7 +31,7 @@ export class DefaultTokenBuilder implements TokenBuilder { terminalTokens.forEach(terminalToken => { const pattern = terminalToken.PATTERN; - if (typeof pattern === 'object' && pattern && 'test' in pattern && isWhitespaceRegExp(pattern)) { + if (typeof pattern === 'object' && pattern && 'test' in pattern && isWhitespace(pattern)) { tokens.unshift(terminalToken); } else { tokens.push(terminalToken); @@ -57,7 +57,7 @@ export class DefaultTokenBuilder implements TokenBuilder { }; if (terminal.hidden) { // Only skip tokens that are able to accept whitespace - tokenType.GROUP = isWhitespaceRegExp(regex) ? Lexer.SKIPPED : 'hidden'; + tokenType.GROUP = isWhitespace(regex) ? Lexer.SKIPPED : 'hidden'; } return tokenType; } diff --git a/packages/langium/src/parser/value-converter.ts b/packages/langium/src/parser/value-converter.ts index 3b5f81e73..9d554c65a 100644 --- a/packages/langium/src/parser/value-converter.ts +++ b/packages/langium/src/parser/value-converter.ts @@ -7,7 +7,7 @@ import type { AbstractElement, AbstractRule } from '../languages/generated/ast.js'; import type { CstNode } from '../syntax-tree.js'; import { isCrossReference, isRuleCall } from '../languages/generated/ast.js'; -import { getCrossReferenceTerminal, getRuleType } from '../utils/grammar-util.js'; +import { getCrossReferenceTerminal, getRuleType } from '../utils/grammar-utils.js'; /** * Language-specific service for converting string values from the source text format into a value to be held in the AST. @@ -41,71 +41,75 @@ export class DefaultValueConverter implements ValueConverter { // eslint-disable-next-line @typescript-eslint/no-unused-vars protected runConverter(rule: AbstractRule, input: string, cstNode: CstNode): ValueType { switch (rule.name.toUpperCase()) { - case 'INT': return convertInt(input); - case 'STRING': return convertString(input); - case 'ID': return convertID(input); + case 'INT': return ValueConverter.convertInt(input); + case 'STRING': return ValueConverter.convertString(input); + case 'ID': return ValueConverter.convertID(input); } switch (getRuleType(rule)?.toLowerCase()) { - case 'number': return convertNumber(input); - case 'boolean': return convertBoolean(input); - case 'bigint': return convertBigint(input); - case 'date': return convertDate(input); + case 'number': return ValueConverter.convertNumber(input); + case 'boolean': return ValueConverter.convertBoolean(input); + case 'bigint': return ValueConverter.convertBigint(input); + case 'date': return ValueConverter.convertDate(input); default: return input; } } } -export function convertString(input: string): string { - let result = ''; - for (let i = 1; i < input.length - 1; i++) { - const c = input.charAt(i); - if (c === '\\') { - const c1 = input.charAt(++i); - result += convertEscapeCharacter(c1); - } else { - result += c; +export namespace ValueConverter { + + export function convertString(input: string): string { + let result = ''; + for (let i = 1; i < input.length - 1; i++) { + const c = input.charAt(i); + if (c === '\\') { + const c1 = input.charAt(++i); + result += convertEscapeCharacter(c1); + } else { + result += c; + } } + return result; } - return result; -} -function convertEscapeCharacter(char: string): string { - switch (char) { - case 'b': return '\b'; - case 'f': return '\f'; - case 'n': return '\n'; - case 'r': return '\r'; - case 't': return '\t'; - case 'v': return '\v'; - case '0': return '\0'; - default: return char; + function convertEscapeCharacter(char: string): string { + switch (char) { + case 'b': return '\b'; + case 'f': return '\f'; + case 'n': return '\n'; + case 'r': return '\r'; + case 't': return '\t'; + case 'v': return '\v'; + case '0': return '\0'; + default: return char; + } } -} -export function convertID(input: string): string { - if (input.charAt(0) === '^') { - return input.substring(1); - } else { - return input; + export function convertID(input: string): string { + if (input.charAt(0) === '^') { + return input.substring(1); + } else { + return input; + } } -} -export function convertInt(input: string): number { - return parseInt(input); -} + export function convertInt(input: string): number { + return parseInt(input); + } -export function convertBigint(input: string): bigint { - return BigInt(input); -} + export function convertBigint(input: string): bigint { + return BigInt(input); + } -export function convertDate(input: string): Date { - return new Date(input); -} + export function convertDate(input: string): Date { + return new Date(input); + } -export function convertNumber(input: string): number { - return Number(input); -} + export function convertNumber(input: string): number { + return Number(input); + } + + export function convertBoolean(input: string): boolean { + return input.toLowerCase() === 'true'; + } -export function convertBoolean(input: string): boolean { - return input.toLowerCase() === 'true'; } diff --git a/packages/langium/src/references/linker.ts b/packages/langium/src/references/linker.ts index cce86ea28..d7995d4ad 100644 --- a/packages/langium/src/references/linker.ts +++ b/packages/langium/src/references/linker.ts @@ -11,8 +11,8 @@ import type { LangiumDocument, LangiumDocuments } from '../workspace/documents.j import type { ScopeProvider } from './scope-provider.js'; import { CancellationToken } from 'vscode-languageserver'; import { isAstNode, isAstNodeDescription, isLinkingError } from '../syntax-tree.js'; -import { getDocument, streamAst, streamReferences } from '../utils/ast-util.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { getDocument, streamAst, streamReferences } from '../utils/ast-utils.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; import { DocumentState } from '../workspace/documents.js'; /** diff --git a/packages/langium/src/references/name-provider.ts b/packages/langium/src/references/name-provider.ts index ae83671ca..209fd4da6 100644 --- a/packages/langium/src/references/name-provider.ts +++ b/packages/langium/src/references/name-provider.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { AstNode, CstNode } from '../syntax-tree.js'; -import { findNodeForProperty } from '../utils/grammar-util.js'; +import { findNodeForProperty } from '../utils/grammar-utils.js'; export interface NamedAstNode extends AstNode { name: string; diff --git a/packages/langium/src/references/references.ts b/packages/langium/src/references/references.ts index c22701fe3..2f3015e28 100644 --- a/packages/langium/src/references/references.ts +++ b/packages/langium/src/references/references.ts @@ -11,13 +11,13 @@ import type { ReferenceDescription } from '../workspace/ast-descriptions.js'; import type { AstNodeLocator } from '../workspace/ast-node-locator.js'; import type { IndexManager } from '../workspace/index-manager.js'; import type { NameProvider } from './name-provider.js'; -import type { URI } from '../utils/uri-util.js'; -import { findAssignment } from '../utils/grammar-util.js'; +import type { URI } from '../utils/uri-utils.js'; +import { findAssignment } from '../utils/grammar-utils.js'; import { isReference } from '../syntax-tree.js'; -import { getDocument } from '../utils/ast-util.js'; -import { isCstChildNode, toDocumentSegment } from '../utils/cst-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { isChildNode, toDocumentSegment } from '../utils/cst-utils.js'; import { stream } from '../utils/stream.js'; -import { UriUtils } from '../utils/uri-util.js'; +import { UriUtils } from '../utils/uri-utils.js'; /** * Language-specific service for finding references and declaration of a given `CstNode`. @@ -96,7 +96,7 @@ export class DefaultReferences implements References { if (nodeElem) { const nameNode = this.nameProvider.getNameNode(nodeElem); // Only return the targeted node in case the targeted cst node is the name node or part of it - if (nameNode && (nameNode === sourceCstNode || isCstChildNode(sourceCstNode, nameNode))) { + if (nameNode && (nameNode === sourceCstNode || isChildNode(sourceCstNode, nameNode))) { return nodeElem; } } diff --git a/packages/langium/src/references/scope-computation.ts b/packages/langium/src/references/scope-computation.ts index 779ec2a4f..9fffcebc5 100644 --- a/packages/langium/src/references/scope-computation.ts +++ b/packages/langium/src/references/scope-computation.ts @@ -10,9 +10,9 @@ import type { AstNodeDescriptionProvider } from '../workspace/ast-descriptions.j import type { LangiumDocument, PrecomputedScopes } from '../workspace/documents.js'; import type { NameProvider } from './name-provider.js'; import { CancellationToken } from 'vscode-jsonrpc'; -import { streamAllContents, streamContents } from '../utils/ast-util.js'; +import { streamAllContents, streamContents } from '../utils/ast-utils.js'; import { MultiMap } from '../utils/collections.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; /** * Language-specific service for precomputing global and local scopes. The service methods are executed diff --git a/packages/langium/src/references/scope-provider.ts b/packages/langium/src/references/scope-provider.ts index 1349e48a0..253e9aaac 100644 --- a/packages/langium/src/references/scope-provider.ts +++ b/packages/langium/src/references/scope-provider.ts @@ -12,7 +12,7 @@ import type { IndexManager } from '../workspace/index-manager.js'; import type { NameProvider } from './name-provider.js'; import type { Scope, ScopeOptions} from './scope.js'; import { MapScope, StreamScope } from './scope.js'; -import { getDocument } from '../utils/ast-util.js'; +import { getDocument } from '../utils/ast-utils.js'; import { stream } from '../utils/stream.js'; import { WorkspaceCache } from '../utils/caching.js'; diff --git a/packages/langium/src/serializer/json-serializer.ts b/packages/langium/src/serializer/json-serializer.ts index 068f94df7..18f6f6b0e 100644 --- a/packages/langium/src/serializer/json-serializer.ts +++ b/packages/langium/src/serializer/json-serializer.ts @@ -6,14 +6,13 @@ import type { NameProvider } from '../references/name-provider.js'; import type { LangiumServices } from '../services.js'; -import type { AstNode, CstNode, GenericAstNode, Reference } from '../syntax-tree.js'; -import type { Mutable } from '../utils/ast-util.js'; +import type { AstNode, CstNode, GenericAstNode, Mutable, Reference } from '../syntax-tree.js'; import type { AstNodeLocator } from '../workspace/ast-node-locator.js'; import type { DocumentSegment, LangiumDocument, LangiumDocuments } from '../workspace/documents.js'; import { URI } from 'vscode-uri'; import { isAstNode, isReference } from '../syntax-tree.js'; -import { getDocument } from '../utils/ast-util.js'; -import { findNodesForProperty } from '../utils/grammar-util.js'; +import { getDocument } from '../utils/ast-utils.js'; +import { findNodesForProperty } from '../utils/grammar-utils.js'; import type { CommentProvider } from '../documentation/comment-provider.js'; export interface JsonSerializeOptions { diff --git a/packages/langium/src/service-registry.ts b/packages/langium/src/service-registry.ts index 8b79a895a..528c5256c 100644 --- a/packages/langium/src/service-registry.ts +++ b/packages/langium/src/service-registry.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { LangiumServices } from './services.js'; -import { UriUtils, type URI } from './utils/uri-util.js'; +import { UriUtils, type URI } from './utils/uri-utils.js'; /** * The service registry provides access to the language-specific services. These are resolved diff --git a/packages/langium/src/syntax-tree.ts b/packages/langium/src/syntax-tree.ts index 10d90dac3..a318cf57a 100644 --- a/packages/langium/src/syntax-tree.ts +++ b/packages/langium/src/syntax-tree.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { TokenType } from 'chevrotain'; -import type { URI } from './utils/uri-util.js'; +import type { URI } from './utils/uri-utils.js'; import type { AbstractElement } from './languages/generated/ast.js'; import type { DocumentSegment, LangiumDocument } from './workspace/documents.js'; @@ -304,3 +304,7 @@ export type AstTypeList = Record; export type AstNodeTypesWithCrossReferences> = { [T in keyof A]: CrossReferencesOfAstNodeType extends never ? never : A[T] }[keyof A]; + +export type Mutable = { + -readonly [P in keyof T]: T[P] +}; diff --git a/packages/langium/src/test/langium-test.ts b/packages/langium/src/test/langium-test.ts index debca53f6..6dab85486 100644 --- a/packages/langium/src/test/langium-test.ts +++ b/packages/langium/src/test/langium-test.ts @@ -10,9 +10,9 @@ import type { AstNode, CstNode, Properties } from '../syntax-tree.js'; import type { LangiumDocument } from '../workspace/documents.js'; import type { BuildOptions } from '../workspace/document-builder.js'; import { DiagnosticSeverity, MarkupContent } from 'vscode-languageserver'; -import { escapeRegExp } from '../utils/regex-util.js'; -import { URI } from '../utils/uri-util.js'; -import { findNodeForProperty } from '../utils/grammar-util.js'; +import { escapeRegExp } from '../utils/regexp-utils.js'; +import { URI } from '../utils/uri-utils.js'; +import { findNodeForProperty } from '../utils/grammar-utils.js'; import { SemanticTokensDecoder } from '../lsp/semantic-token-provider.js'; import { TextDocument } from 'vscode-languageserver-textdocument'; import * as assert from 'node:assert'; diff --git a/packages/langium/src/utils/ast-util.ts b/packages/langium/src/utils/ast-utils.ts similarity index 96% rename from packages/langium/src/utils/ast-util.ts rename to packages/langium/src/utils/ast-utils.ts index cc63c8f9f..cb915be82 100644 --- a/packages/langium/src/utils/ast-util.ts +++ b/packages/langium/src/utils/ast-utils.ts @@ -5,16 +5,12 @@ ******************************************************************************/ import type { Range } from 'vscode-languageserver'; -import type { AstNode, AstReflection, CstNode, GenericAstNode, PropertyType, Reference, ReferenceInfo } from '../syntax-tree.js'; -import type { Stream, TreeStream } from '../utils/stream.js'; +import type { AstNode, AstReflection, CstNode, GenericAstNode, Mutable, PropertyType, Reference, ReferenceInfo } from '../syntax-tree.js'; +import type { Stream, TreeStream } from './stream.js'; import type { LangiumDocument } from '../workspace/documents.js'; import { isAstNode, isReference } from '../syntax-tree.js'; -import { DONE_RESULT, stream, StreamImpl, TreeStreamImpl } from '../utils/stream.js'; -import { inRange } from './cst-util.js'; - -export type Mutable = { - -readonly [P in keyof T]: T[P] -}; +import { DONE_RESULT, stream, StreamImpl, TreeStreamImpl } from './stream.js'; +import { inRange } from './cst-utils.js'; /** * Link the `$container` and other related properties of every AST node that is directly contained @@ -238,7 +234,7 @@ export function findLocalReferences(targetNode: AstNode, lookup = getDocument(ta * @param reflection Reflection object used to gather mandatory properties for the node. * @param node Specified node is modified in place and properties are directly assigned. */ -export function assignMandatoryAstProperties(reflection: AstReflection, node: AstNode): void { +export function assignMandatoryProperties(reflection: AstReflection, node: AstNode): void { const typeMetaData = reflection.getTypeMetaData(node.$type); const genericNode = node as GenericAstNode; for (const property of typeMetaData.properties) { diff --git a/packages/langium/src/utils/caching.ts b/packages/langium/src/utils/caching.ts index 2c9c69ebf..e42421ae7 100644 --- a/packages/langium/src/utils/caching.ts +++ b/packages/langium/src/utils/caching.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import type { Disposable } from './disposable.js'; -import type { URI } from './uri-util.js'; +import type { URI } from './uri-utils.js'; import type { LangiumSharedServices } from '../services.js'; export abstract class DisposableCache implements Disposable { diff --git a/packages/langium/src/utils/cst-util.ts b/packages/langium/src/utils/cst-utils.ts similarity index 99% rename from packages/langium/src/utils/cst-util.ts rename to packages/langium/src/utils/cst-utils.ts index 094e346c2..59e6b1a5c 100644 --- a/packages/langium/src/utils/cst-util.ts +++ b/packages/langium/src/utils/cst-utils.ts @@ -36,7 +36,7 @@ export function flattenCst(node: CstNode): Stream { /** * Determines whether the specified cst node is a child of the specified parent node. */ -export function isCstChildNode(child: CstNode, parent: CstNode): boolean { +export function isChildNode(child: CstNode, parent: CstNode): boolean { while (child.container) { child = child.container; if (child === parent) { diff --git a/packages/langium/src/utils/grammar-loader.ts b/packages/langium/src/utils/grammar-loader.ts index ef6b69379..8ab590f7f 100644 --- a/packages/langium/src/utils/grammar-loader.ts +++ b/packages/langium/src/utils/grammar-loader.ts @@ -4,14 +4,14 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { Mutable } from './ast-util.js'; import type { Module } from '../dependency-injection.js'; import type { LangiumServices, LangiumSharedServices, PartialLangiumServices, PartialLangiumSharedServices } from '../services.js'; +import type { Mutable } from '../syntax-tree.js'; import * as ast from '../languages/generated/ast.js'; import { inject } from '../dependency-injection.js'; import { createDefaultModule, createDefaultSharedModule } from '../default-module.js'; import { EmptyFileSystem } from '../workspace/file-system-provider.js'; -import { URI } from './uri-util.js'; +import { URI } from './uri-utils.js'; const minimalGrammarModule: Module = { Grammar: () => undefined as unknown as ast.Grammar, diff --git a/packages/langium/src/utils/grammar-util.ts b/packages/langium/src/utils/grammar-utils.ts similarity index 99% rename from packages/langium/src/utils/grammar-util.ts rename to packages/langium/src/utils/grammar-utils.ts index 5512d8267..563e4648d 100644 --- a/packages/langium/src/utils/grammar-util.ts +++ b/packages/langium/src/utils/grammar-utils.ts @@ -7,9 +7,9 @@ import type { AstNode, CstNode } from '../syntax-tree.js'; import * as ast from '../languages/generated/ast.js'; import { isCompositeCstNode } from '../syntax-tree.js'; -import { getContainerOfType, streamAllContents } from '../utils/ast-util.js'; -import { streamCst } from '../utils/cst-util.js'; -import { escapeRegExp } from './regex-util.js'; +import { getContainerOfType, streamAllContents } from './ast-utils.js'; +import { streamCst } from './cst-utils.js'; +import { escapeRegExp } from './regexp-utils.js'; /** * Returns the entry rule of the given grammar, if any. If the grammar file does not contain an entry rule, diff --git a/packages/langium/src/utils/index.ts b/packages/langium/src/utils/index.ts index fb3995981..123e84539 100644 --- a/packages/langium/src/utils/index.ts +++ b/packages/langium/src/utils/index.ts @@ -4,15 +4,17 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -export * from './ast-util.js'; export * from './caching.js'; export * from './collections.js'; -export * from './cst-util.js'; export * from './disposable.js'; export * from './errors.js'; export * from './grammar-loader.js'; -export * from './grammar-util.js'; -export * from './promise-util.js'; -export * from './regex-util.js'; +export * from './promise-utils.js'; export * from './stream.js'; -export * from './uri-util.js'; +export * from './uri-utils.js'; + +import * as AstUtils from './ast-utils.js'; +import * as CstUtils from './cst-utils.js'; +import * as GrammarUtils from './grammar-utils.js'; +import * as RegExpUtils from './regexp-utils.js'; +export { AstUtils, CstUtils, GrammarUtils, RegExpUtils }; diff --git a/packages/langium/src/utils/promise-util.ts b/packages/langium/src/utils/promise-utils.ts similarity index 100% rename from packages/langium/src/utils/promise-util.ts rename to packages/langium/src/utils/promise-utils.ts diff --git a/packages/langium/src/utils/regex-util.ts b/packages/langium/src/utils/regexp-utils.ts similarity index 86% rename from packages/langium/src/utils/regex-util.ts rename to packages/langium/src/utils/regexp-utils.ts index d7b58646e..b2d05f995 100644 --- a/packages/langium/src/utils/regex-util.ts +++ b/packages/langium/src/utils/regexp-utils.ts @@ -9,45 +9,45 @@ import { RegExpParser, BaseRegExpVisitor } from '@chevrotain/regexp-to-ast'; export const NEWLINE_REGEXP = /\r?\n/gm; -const regexParser = new RegExpParser(); +const regexpParser = new RegExpParser(); /** * This class is in charge of heuristically identifying start/end tokens of terminals. * * The way this works is by doing the following: * 1. Traverse the regular expression in the "start state" - * 2. Add any encountered sets/single characters to the "start regex" + * 2. Add any encountered sets/single characters to the "start regexp" * 3. Once we encounter any variable-length content (i.e. with quantifiers such as +/?/*), we enter the "end state" * 4. In the end state, any sets/single characters are added to an "end stack". * 5. If we re-encounter any variable-length content we reset the end stack * 6. We continue visiting the regex until the end, reseting the end stack and rebuilding it as necessary * - * After traversing a regular expression the `startRegex/endRegex` properties allow access to the stored start/end of the terminal + * After traversing a regular expression the `startRegexp/endRegexp` properties allow access to the stored start/end of the terminal */ -class TerminalRegexVisitor extends BaseRegExpVisitor { +class TerminalRegExpVisitor extends BaseRegExpVisitor { private isStarting = true; - startRegex: string; - private endRegexStack: string[] = []; + startRegexp: string; + private endRegexpStack: string[] = []; multiline = false; regex: string; get endRegex(): string { - return this.endRegexStack.join(''); + return this.endRegexpStack.join(''); } reset(regex: string): void { this.multiline = false; this.regex = regex; - this.startRegex = ''; + this.startRegexp = ''; this.isStarting = true; - this.endRegexStack = []; + this.endRegexpStack = []; } override visitGroup(node: Group) { if (node.quantifier) { this.isStarting = false; - this.endRegexStack = []; + this.endRegexpStack = []; } } @@ -58,12 +58,12 @@ class TerminalRegexVisitor extends BaseRegExpVisitor { } if (node.quantifier) { this.isStarting = false; - this.endRegexStack = []; + this.endRegexpStack = []; } else { const escapedChar = escapeRegExp(char); - this.endRegexStack.push(escapedChar); + this.endRegexpStack.push(escapedChar); if (this.isStarting) { - this.startRegex += escapedChar; + this.startRegexp += escapedChar; } } } @@ -76,12 +76,12 @@ class TerminalRegexVisitor extends BaseRegExpVisitor { } if (node.quantifier) { this.isStarting = false; - this.endRegexStack = []; + this.endRegexpStack = []; } else { const set = this.regex.substring(node.loc.begin, node.loc.end); - this.endRegexStack.push(set); + this.endRegexpStack.push(set); if (this.isStarting) { - this.startRegex += set; + this.startRegexp += set; } } } @@ -99,21 +99,21 @@ class TerminalRegexVisitor extends BaseRegExpVisitor { } } -const visitor = new TerminalRegexVisitor(); +const visitor = new TerminalRegExpVisitor(); -export function getTerminalParts(regex: RegExp | string): Array<{ start: string, end: string }> { +export function getTerminalParts(regexp: RegExp | string): Array<{ start: string, end: string }> { try { - if (typeof regex !== 'string') { - regex = regex.source; + if (typeof regexp !== 'string') { + regexp = regexp.source; } - regex = `/${regex}/`; - const pattern = regexParser.pattern(regex); + regexp = `/${regexp}/`; + const pattern = regexpParser.pattern(regexp); const parts: Array<{ start: string, end: string }> = []; for (const alternative of pattern.value.value) { - visitor.reset(regex); + visitor.reset(regexp); visitor.visit(alternative); parts.push({ - start: visitor.startRegex, + start: visitor.startRegexp, end: visitor.endRegex }); } @@ -123,22 +123,22 @@ export function getTerminalParts(regex: RegExp | string): Array<{ start: string, } } -export function isMultilineComment(regex: RegExp | string): boolean { +export function isMultilineComment(regexp: RegExp | string): boolean { try { - if (typeof regex === 'string') { - regex = new RegExp(regex); + if (typeof regexp === 'string') { + regexp = new RegExp(regexp); } - regex = regex.toString(); - visitor.reset(regex); + regexp = regexp.toString(); + visitor.reset(regexp); // Parsing the pattern might fail (since it's user code) - visitor.visit(regexParser.pattern(regex)); + visitor.visit(regexpParser.pattern(regexp)); return visitor.multiline; } catch { return false; } } -export function isWhitespaceRegExp(value: RegExp | string): boolean { +export function isWhitespace(value: RegExp | string): boolean { const regexp = typeof value === 'string' ? new RegExp(value) : value; return regexp.test(' '); } @@ -160,7 +160,7 @@ export function getCaseInsensitivePattern(keyword: string): string { * @returns Whether any match exists. */ export function partialMatches(regex: RegExp | string, input: string): boolean { - const partial = partialRegex(regex); + const partial = partialRegExp(regex); const match = input.match(partial); return !!match && match[0].length > 0; } @@ -171,7 +171,7 @@ export function partialMatches(regex: RegExp | string, input: string): boolean { * @param regex The input regex to be converted. * @returns A partial regex constructed from the input regex. */ -export function partialRegex(regex: RegExp | string): RegExp { +export function partialRegExp(regex: RegExp | string): RegExp { if (typeof regex === 'string') { regex = new RegExp(regex); } diff --git a/packages/langium/src/utils/uri-util.ts b/packages/langium/src/utils/uri-utils.ts similarity index 87% rename from packages/langium/src/utils/uri-util.ts rename to packages/langium/src/utils/uri-utils.ts index 44d349d1b..c7d0d5760 100644 --- a/packages/langium/src/utils/uri-util.ts +++ b/packages/langium/src/utils/uri-utils.ts @@ -37,12 +37,3 @@ export namespace UriUtils { } } - -/** - * @deprecated Use `UriUtils.equals` instead. - */ -export const equalURI = UriUtils.equals; -/** - * @deprecated Use `UriUtils.relative` instead. - */ -export const relativeURI = UriUtils.relative; diff --git a/packages/langium/src/validation/document-validator.ts b/packages/langium/src/validation/document-validator.ts index c4123276b..4ca0be16b 100644 --- a/packages/langium/src/validation/document-validator.ts +++ b/packages/langium/src/validation/document-validator.ts @@ -13,10 +13,10 @@ import type { AstNode, CstNode } from '../syntax-tree.js'; import type { LangiumDocument } from '../workspace/documents.js'; import type { DiagnosticData, DiagnosticInfo, ValidationAcceptor, ValidationCategory, ValidationRegistry } from './validation-registry.js'; import { CancellationToken, DiagnosticSeverity, Position, Range } from 'vscode-languageserver'; -import { findNodeForKeyword, findNodeForProperty } from '../utils/grammar-util.js'; -import { streamAst } from '../utils/ast-util.js'; -import { tokenToRange } from '../utils/cst-util.js'; -import { interruptAndCheck, isOperationCancelled } from '../utils/promise-util.js'; +import { findNodeForKeyword, findNodeForProperty } from '../utils/grammar-utils.js'; +import { streamAst } from '../utils/ast-utils.js'; +import { tokenToRange } from '../utils/cst-utils.js'; +import { interruptAndCheck, isOperationCancelled } from '../utils/promise-utils.js'; import { diagnosticData } from './validation-registry.js'; export interface ValidationOptions { diff --git a/packages/langium/src/validation/validation-registry.ts b/packages/langium/src/validation/validation-registry.ts index 97c3b9dc5..787bd02de 100644 --- a/packages/langium/src/validation/validation-registry.ts +++ b/packages/langium/src/validation/validation-registry.ts @@ -7,11 +7,11 @@ import type { CancellationToken, CodeDescription, DiagnosticRelatedInformation, DiagnosticTag, integer, Range } from 'vscode-languageserver'; import type { LangiumServices } from '../services.js'; import type { AstNode, AstReflection, Properties } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { Stream } from '../utils/stream.js'; import type { DocumentSegment } from '../workspace/documents.js'; import { MultiMap } from '../utils/collections.js'; -import { isOperationCancelled } from '../utils/promise-util.js'; +import { isOperationCancelled } from '../utils/promise-utils.js'; import { stream } from '../utils/stream.js'; export type DiagnosticInfo> = { @@ -49,6 +49,9 @@ export interface DiagnosticData { actionRange?: Range } +/** + * Create DiagnosticData for a given diagnostic code. The result can be put into the `data` field of a DiagnosticInfo. + */ export function diagnosticData(code: string): DiagnosticData { return { code }; } diff --git a/packages/langium/src/workspace/ast-descriptions.ts b/packages/langium/src/workspace/ast-descriptions.ts index 8aace3200..a378fafa2 100644 --- a/packages/langium/src/workspace/ast-descriptions.ts +++ b/packages/langium/src/workspace/ast-descriptions.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { URI } from '../utils/uri-util.js'; +import type { URI } from '../utils/uri-utils.js'; import type { NameProvider } from '../references/name-provider.js'; import type { LangiumServices } from '../services.js'; import type { AstNode, AstNodeDescription, ReferenceInfo } from '../syntax-tree.js'; @@ -12,10 +12,10 @@ import type { AstNodeLocator } from './ast-node-locator.js'; import type { DocumentSegment, LangiumDocument } from './documents.js'; import { CancellationToken } from 'vscode-languageserver'; import { isLinkingError } from '../syntax-tree.js'; -import { getDocument, streamAst, streamReferences } from '../utils/ast-util.js'; -import { toDocumentSegment } from '../utils/cst-util.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; -import { UriUtils } from '../utils/uri-util.js'; +import { getDocument, streamAst, streamReferences } from '../utils/ast-utils.js'; +import { toDocumentSegment } from '../utils/cst-utils.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; +import { UriUtils } from '../utils/uri-utils.js'; /** * Language-specific service for creating descriptions of AST nodes to be used for cross-reference resolutions. diff --git a/packages/langium/src/workspace/document-builder.ts b/packages/langium/src/workspace/document-builder.ts index dc2f16de0..b602abd8a 100644 --- a/packages/langium/src/workspace/document-builder.ts +++ b/packages/langium/src/workspace/document-builder.ts @@ -4,17 +4,17 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { URI } from '../utils/uri-util.js'; +import type { URI } from '../utils/uri-utils.js'; import type { ServiceRegistry } from '../service-registry.js'; import type { LangiumSharedServices } from '../services.js'; import type { AstNode } from '../syntax-tree.js'; -import type { MaybePromise } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; import type { ValidationOptions } from '../validation/document-validator.js'; import type { IndexManager } from '../workspace/index-manager.js'; import type { LangiumDocument, LangiumDocuments, LangiumDocumentFactory } from './documents.js'; import { CancellationToken, Disposable } from 'vscode-languageserver'; import { MultiMap } from '../utils/collections.js'; -import { interruptAndCheck } from '../utils/promise-util.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; import { stream } from '../utils/stream.js'; import { ValidationCategory } from '../validation/validation-registry.js'; import { DocumentState } from './documents.js'; diff --git a/packages/langium/src/workspace/documents.ts b/packages/langium/src/workspace/documents.ts index ca5e8f8b2..999a01942 100644 --- a/packages/langium/src/workspace/documents.ts +++ b/packages/langium/src/workspace/documents.ts @@ -9,12 +9,11 @@ import { CancellationToken } from 'vscode-languageserver'; import type { ParseResult } from '../parser/langium-parser.js'; import type { ServiceRegistry } from '../service-registry.js'; import type { LangiumSharedServices } from '../services.js'; -import type { AstNode, AstNodeDescription, Reference } from '../syntax-tree.js'; -import type { Mutable } from '../utils/ast-util.js'; +import type { AstNode, AstNodeDescription, Mutable, Reference } from '../syntax-tree.js'; import type { MultiMap } from '../utils/collections.js'; import type { Stream } from '../utils/stream.js'; import type { FileSystemProvider } from './file-system-provider.js'; -import { URI } from '../utils/uri-util.js'; +import { URI } from '../utils/uri-utils.js'; import { TextDocument } from 'vscode-languageserver-textdocument'; import { stream } from '../utils/stream.js'; diff --git a/packages/langium/src/workspace/file-system-provider.ts b/packages/langium/src/workspace/file-system-provider.ts index 60e2197c0..0c7a58d6c 100644 --- a/packages/langium/src/workspace/file-system-provider.ts +++ b/packages/langium/src/workspace/file-system-provider.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { URI } from '../utils/uri-util.js'; +import type { URI } from '../utils/uri-utils.js'; export interface FileSystemNode { readonly isFile: boolean; diff --git a/packages/langium/src/workspace/index-manager.ts b/packages/langium/src/workspace/index-manager.ts index 63d064d88..d4a769de7 100644 --- a/packages/langium/src/workspace/index-manager.ts +++ b/packages/langium/src/workspace/index-manager.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import type { URI } from '../utils/uri-util.js'; +import type { URI } from '../utils/uri-utils.js'; import type { ServiceRegistry } from '../service-registry.js'; import type { LangiumSharedServices } from '../services.js'; import type { AstNode, AstNodeDescription, AstReflection } from '../syntax-tree.js'; @@ -12,9 +12,9 @@ import type { Stream } from '../utils/stream.js'; import type { ReferenceDescription } from './ast-descriptions.js'; import type { LangiumDocument, LangiumDocuments } from './documents.js'; import { CancellationToken } from 'vscode-languageserver'; -import { getDocument } from '../utils/ast-util.js'; +import { getDocument } from '../utils/ast-utils.js'; import { stream } from '../utils/stream.js'; -import { UriUtils } from '../utils/uri-util.js'; +import { UriUtils } from '../utils/uri-utils.js'; import { ContextCache } from '../utils/caching.js'; /** diff --git a/packages/langium/src/workspace/workspace-lock.ts b/packages/langium/src/workspace/workspace-lock.ts index facf086e1..cbf3fa9c5 100644 --- a/packages/langium/src/workspace/workspace-lock.ts +++ b/packages/langium/src/workspace/workspace-lock.ts @@ -5,8 +5,8 @@ ******************************************************************************/ import { CancellationToken, CancellationTokenSource } from 'vscode-languageserver'; -import type { MaybePromise } from '../utils/promise-util.js'; -import { Deferred, isOperationCancelled } from '../utils/promise-util.js'; +import type { MaybePromise } from '../utils/promise-utils.js'; +import { Deferred, isOperationCancelled } from '../utils/promise-utils.js'; /** * Utility service to execute mutually exclusive actions. diff --git a/packages/langium/src/workspace/workspace-manager.ts b/packages/langium/src/workspace/workspace-manager.ts index 3bc7b3163..d9d4b9e59 100644 --- a/packages/langium/src/workspace/workspace-manager.ts +++ b/packages/langium/src/workspace/workspace-manager.ts @@ -5,8 +5,8 @@ ******************************************************************************/ import { CancellationToken } from 'vscode-languageserver'; -import { interruptAndCheck } from '../utils/promise-util.js'; -import { URI, UriUtils } from '../utils/uri-util.js'; +import { interruptAndCheck } from '../utils/promise-utils.js'; +import { URI, UriUtils } from '../utils/uri-utils.js'; import type { WorkspaceFolder } from 'vscode-languageserver'; import type { ServiceRegistry } from '../service-registry.js'; import type { LangiumSharedServices } from '../services.js'; diff --git a/packages/langium/test/documentation/comment-provider.test.ts b/packages/langium/test/documentation/comment-provider.test.ts index 84d802178..f01ace249 100644 --- a/packages/langium/test/documentation/comment-provider.test.ts +++ b/packages/langium/test/documentation/comment-provider.test.ts @@ -6,7 +6,7 @@ import { describe, expect, test } from 'vitest'; import { parseHelper } from 'langium/test'; -import { streamAst, EmptyFileSystem, GrammarAST } from 'langium'; +import { AstUtils, EmptyFileSystem, GrammarAST } from 'langium'; import { createLangiumGrammarServices } from 'langium/grammar'; const services = createLangiumGrammarServices(EmptyFileSystem).grammar; @@ -25,7 +25,7 @@ describe('Comment provider', () => { expect(ast).toBeDefined(); const grammarComment = services.documentation.CommentProvider.getComment(ast); expect(grammarComment).toBeUndefined(); - streamAst(ast).filter(GrammarAST.isAbstractRule).forEach(rule => { + AstUtils.streamAst(ast).filter(GrammarAST.isAbstractRule).forEach(rule => { const comment = services.documentation.CommentProvider.getComment(rule); expect(comment).toBe(`/** ${rule.name} */`); }); diff --git a/packages/langium/test/generate/generation-tracing.test.ts b/packages/langium/test/generate/generation-tracing.test.ts index 40f7752ac..69ba2720e 100644 --- a/packages/langium/test/generate/generation-tracing.test.ts +++ b/packages/langium/test/generate/generation-tracing.test.ts @@ -4,7 +4,7 @@ * terms of the MIT License, which is available in the project root. ******************************************************************************/ -import { TreeStreamImpl, findNodeForKeyword, findNodesForProperty, type AstNode, type AstNodeWithTextRegion } from 'langium'; +import { TreeStreamImpl, GrammarUtils, type AstNode, type AstNodeWithTextRegion } from 'langium'; import { expandToNode, expandToString, expandTracedToNode, joinTracedToNode, joinTracedToNodeIf, toStringAndTrace, traceToNode, type SourceRegion, type TraceRegion } from 'langium/generate'; import { parseHelper } from 'langium/test'; import { createServicesForGrammar } from 'langium/grammar'; @@ -153,8 +153,8 @@ describe('tracing based on provided text regions', () => { expandTracedToNode(source.$cstNode)` beginning ${ joinTracedToNodeIf(source.children.length > 0, () => [ - findNodeForKeyword(source.$cstNode, '{')!, - findNodeForKeyword(source.$cstNode, '}')! + GrammarUtils.findNodeForKeyword(source.$cstNode, '{')!, + GrammarUtils.findNodeForKeyword(source.$cstNode, '}')! ] )(source.children, c => c.name) } end ` @@ -177,7 +177,7 @@ describe('tracing based on provided text regions', () => { const { text, trace } = toStringAndTrace( expandTracedToNode(source.$cstNode)` beginning ${ - joinTracedToNodeIf(source.values.length > 0, () => findNodesForProperty(source.$cstNode, 'values'))(source.values) + joinTracedToNodeIf(source.values.length > 0, () => GrammarUtils.findNodesForProperty(source.$cstNode, 'values'))(source.values) } end ` ); @@ -200,8 +200,8 @@ describe('tracing based on provided text regions', () => { expandTracedToNode(source.$cstNode)` beginning ${ joinTracedToNodeIf(!!source.children /* incomplete condition, array is always initialized, but possibly empty */, () => [ - findNodeForKeyword(source.$cstNode, '{')!, - findNodeForKeyword(source.$cstNode, '}')! + GrammarUtils.findNodeForKeyword(source.$cstNode, '{')!, + GrammarUtils.findNodeForKeyword(source.$cstNode, '}')! ] )(source.children!, c => c.name) } end ` diff --git a/packages/langium/test/grammar/grammar-util.test.ts b/packages/langium/test/grammar/grammar-util.test.ts index 18e6a0888..e866a3fed 100644 --- a/packages/langium/test/grammar/grammar-util.test.ts +++ b/packages/langium/test/grammar/grammar-util.test.ts @@ -5,12 +5,13 @@ ******************************************************************************/ import type { Grammar, GrammarAST as GrammarTypes} from 'langium'; -import { getTypeName, isDataTypeRule, terminalRegex } from 'langium'; import { beforeAll, describe, expect, test } from 'vitest'; -import { EmptyFileSystem, findNameAssignment, getEntryRule, stream, GrammarAST } from 'langium'; +import { EmptyFileSystem, GrammarUtils, stream, GrammarAST } from 'langium'; import { LangiumGrammarGrammar, createLangiumGrammarServices } from 'langium/grammar'; import { parseHelper } from 'langium/test'; +const { isDataTypeRule, findNameAssignment, getEntryRule, getTypeName, terminalRegex } = GrammarUtils; + describe('Data type rules', () => { const services = createLangiumGrammarServices(EmptyFileSystem); diff --git a/packages/langium/test/grammar/grammar-validator.test.ts b/packages/langium/test/grammar/grammar-validator.test.ts index a11e867a8..06c4e3349 100644 --- a/packages/langium/test/grammar/grammar-validator.test.ts +++ b/packages/langium/test/grammar/grammar-validator.test.ts @@ -9,7 +9,7 @@ import type { GrammarAST as GrammarTypes } from 'langium'; import type { ValidationResult } from 'langium/test'; import { afterEach, beforeAll, describe, expect, test } from 'vitest'; import { DiagnosticSeverity } from 'vscode-languageserver'; -import { EmptyFileSystem, GrammarAST, streamAllContents, streamContents } from 'langium'; +import { AstUtils, EmptyFileSystem, GrammarAST } from 'langium'; import { IssueCodes, createLangiumGrammarServices } from 'langium/grammar'; import { clearDocuments, expectError, expectIssue, expectNoIssues, expectWarning, parseHelper, validationHelper } from 'langium/test'; @@ -215,7 +215,7 @@ describe('checkReferenceToRuleButNotType', () => { }); test('CrossReference validation', () => { - const crossRef = streamAllContents(validationResult.document.parseResult.value).find(GrammarAST.isCrossReference)!; + const crossRef = AstUtils.streamAllContents(validationResult.document.parseResult.value).find(GrammarAST.isCrossReference)!; expectError(validationResult, "Could not resolve reference to AbstractType named 'Definition'.", { node: crossRef, property: 'type' @@ -451,7 +451,7 @@ describe('Reserved names', () => { function expectReservedName(validation: ValidationResult, predicate: (node: AstNode) => node is T, property: Properties): void { expect(validation.diagnostics).toHaveLength(1); - const node = streamAllContents(validation.document.parseResult.value).find(predicate)!; + const node = AstUtils.streamAllContents(validation.document.parseResult.value).find(predicate)!; expectIssue(validation, { node, message: / is a reserved name of the JavaScript runtime\.$/, @@ -632,7 +632,7 @@ describe('Property type is not a mix of cross-ref and non-cross-ref types.', () prop = ('string' | [Rule]) ; `); - const rule1Assignment = streamContents(validation.document.parseResult.value.rules[1]) + const rule1Assignment = AstUtils.streamContents(validation.document.parseResult.value.rules[1]) .filter(node => GrammarAST.isAssignment(node)).head() as GrammarTypes.Assignment; expect(rule1Assignment).not.toBe(undefined); @@ -650,7 +650,7 @@ describe('Property type is not a mix of cross-ref and non-cross-ref types.', () prop = ('int' | ('string' | [Rule])) ; `); - const rule1Assignment = streamContents(validation.document.parseResult.value.rules[1]) + const rule1Assignment = AstUtils.streamContents(validation.document.parseResult.value.rules[1]) .filter(node => GrammarAST.isAssignment(node)).head() as GrammarTypes.Assignment; expect(rule1Assignment).not.toBe(undefined); diff --git a/packages/langium/test/grammar/type-system/type-validator.test.ts b/packages/langium/test/grammar/type-system/type-validator.test.ts index 23bd6ada9..690e31821 100644 --- a/packages/langium/test/grammar/type-system/type-validator.test.ts +++ b/packages/langium/test/grammar/type-system/type-validator.test.ts @@ -7,7 +7,7 @@ import type { Diagnostic } from 'vscode-languageserver'; import { describe, expect, test } from 'vitest'; import { DiagnosticSeverity } from 'vscode-languageserver'; -import { EmptyFileSystem, streamAllContents, streamContents, GrammarAST } from 'langium'; +import { AstUtils, EmptyFileSystem, GrammarAST } from 'langium'; import { createLangiumGrammarServices } from 'langium/grammar'; import { expectError, expectNoIssues, parseDocument, validationHelper } from 'langium/test'; @@ -508,7 +508,7 @@ describe('Property type is not a mix of cross-ref and non-cross-ref types.', () terminal ID: /[_a-zA-Z][\\w_]*/; `); - const rule1Assignment = streamContents(validation.document.parseResult.value.rules[1]) + const rule1Assignment = AstUtils.streamContents(validation.document.parseResult.value.rules[1]) .filter(node => GrammarAST.isAssignment(node)).head() as GrammarAST.Assignment; expect(rule1Assignment).not.toBe(undefined); @@ -524,7 +524,7 @@ describe('Property type is not a mix of cross-ref and non-cross-ref types.', () ; terminal ID: /[_a-zA-Z][\\w_]*/; `); - const propAssignments = streamAllContents(validation.document.parseResult.value.rules[0]) + const propAssignments = AstUtils.streamAllContents(validation.document.parseResult.value.rules[0]) .filter(node => GrammarAST.isAssignment(node)).toArray(); expect(propAssignments.length).toBe(2); @@ -688,7 +688,7 @@ describe('Property types validation takes in account types hierarchy', () => { terminal NUMBER returns number: /[0-9]+(\\.[0-9]*)?/; `); - const assignment = streamAllContents(validation.document.parseResult.value).filter(GrammarAST.isAssignment).toArray()[0]; + const assignment = AstUtils.streamAllContents(validation.document.parseResult.value).filter(GrammarAST.isAssignment).toArray()[0]; expectError(validation, "The assigned type 'Z2' is not compatible with the declared property 'y' of type 'Z1'.", { node: assignment, property: 'feature' diff --git a/packages/langium/test/utils/ast-util.test.ts b/packages/langium/test/utils/ast-utils.test.ts similarity index 90% rename from packages/langium/test/utils/ast-util.test.ts rename to packages/langium/test/utils/ast-utils.test.ts index 4217d1b41..2175bc40f 100644 --- a/packages/langium/test/utils/ast-util.test.ts +++ b/packages/langium/test/utils/ast-utils.test.ts @@ -5,7 +5,7 @@ ******************************************************************************/ import { describe, expect, test } from 'vitest'; -import { EmptyFileSystem, streamAst, GrammarAST } from 'langium'; +import { AstUtils, EmptyFileSystem, GrammarAST } from 'langium'; import { createLangiumGrammarServices } from 'langium/grammar'; describe('AST Utils', () => { @@ -33,7 +33,7 @@ describe('AST Utils', () => { } }; const names: string[] = []; - for (const node of streamAst(parseResult.value, options)) { + for (const node of AstUtils.streamAst(parseResult.value, options)) { if (GrammarAST.isParserRule(node)) { names.push(node.name); } diff --git a/packages/langium/test/utils/cst-utils.test.ts b/packages/langium/test/utils/cst-utils.test.ts index 712bf5db6..219a2c213 100644 --- a/packages/langium/test/utils/cst-utils.test.ts +++ b/packages/langium/test/utils/cst-utils.test.ts @@ -5,12 +5,14 @@ ******************************************************************************/ import type { Grammar, LeafCstNode } from 'langium'; -import { EmptyFileSystem, findLeafNodeAtOffset, findLeafNodeBeforeOffset } from 'langium'; +import { CstUtils, EmptyFileSystem } from 'langium'; import { expandToString } from 'langium/generate'; import { createLangiumGrammarServices } from 'langium/grammar'; import { parseHelper } from 'langium/test'; import { describe, expect, test } from 'vitest'; +const { findLeafNodeAtOffset, findLeafNodeBeforeOffset } = CstUtils; + const services = createLangiumGrammarServices(EmptyFileSystem); const parser = parseHelper(services.grammar); diff --git a/packages/langium/test/utils/grammar-util.test.ts b/packages/langium/test/utils/grammar-utils.test.ts similarity index 88% rename from packages/langium/test/utils/grammar-util.test.ts rename to packages/langium/test/utils/grammar-utils.test.ts index 7c0bab45f..eb5e2795c 100644 --- a/packages/langium/test/utils/grammar-util.test.ts +++ b/packages/langium/test/utils/grammar-utils.test.ts @@ -6,7 +6,7 @@ import type { Grammar } from 'langium'; import { describe, expect, test } from 'vitest'; -import { EmptyFileSystem, getAllReachableRules } from 'langium'; +import { EmptyFileSystem, GrammarUtils } from 'langium'; import { createLangiumGrammarServices } from 'langium/grammar'; import { parseHelper } from 'langium/test'; @@ -31,7 +31,7 @@ describe('Grammar Utils', () => { const output = await parse(input); // act - const reachableRules = [...getAllReachableRules(output.parseResult.value, true)].map(r => r.name); + const reachableRules = [...GrammarUtils.getAllReachableRules(output.parseResult.value, true)].map(r => r.name); // assert expect(reachableRules).toContain('Ws'); diff --git a/packages/langium/test/utils/regex-util.test.ts b/packages/langium/test/utils/regexp-utils.test.ts similarity index 93% rename from packages/langium/test/utils/regex-util.test.ts rename to packages/langium/test/utils/regexp-utils.test.ts index 8220c1ba3..470619bd7 100644 --- a/packages/langium/test/utils/regex-util.test.ts +++ b/packages/langium/test/utils/regexp-utils.test.ts @@ -5,9 +5,11 @@ ******************************************************************************/ import { describe, expect, test } from 'vitest'; -import { getTerminalParts, isMultilineComment, partialMatches } from 'langium'; +import { RegExpUtils } from 'langium'; -describe('partial regex', () => { +const { getTerminalParts, isMultilineComment, partialMatches } = RegExpUtils; + +describe('Partial regexp', () => { test('should match /ab/ string with "a"', () => { expect(partialMatches('^ab', 'a')).toBeTruthy(); @@ -71,7 +73,7 @@ describe('partial regex', () => { }); -describe('multiline comment detection', () => { +describe('Multiline comment detection', () => { test('single character string should be not multiline comment', () => { expect(isMultilineComment('x')).toBeFalsy(); @@ -95,7 +97,7 @@ describe('multiline comment detection', () => { }); -describe('comment start/end parts', () => { +describe('Comment start/end parts', () => { test('JS style singleline comment should start with //', () => { expect(getTerminalParts(/\/\/[^\n\r]*/)).toEqual([{