Skip to content

Commit

Permalink
Move getActionAtElement
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Sep 28, 2023
1 parent 80a51c0 commit 9d6a154
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
26 changes: 1 addition & 25 deletions packages/langium/src/grammar/internal-grammar-util.ts
Expand Up @@ -8,7 +8,7 @@ import { URI } from '../utils/uri-util.js';
import type { LangiumDocuments } from '../workspace/documents.js';
import type { AstNode } from '../syntax-tree.js';
import * as ast from '../grammar/generated/ast.js';
import { getDocument, streamAllContents } from '../utils/ast-util.js';
import { getDocument } from '../utils/ast-util.js';
import { UriUtils } from '../utils/uri-util.js';
import type { LangiumGrammarServices} from './langium-grammar-module.js';
import { createLangiumGrammarServices } from './langium-grammar-module.js';
Expand Down Expand Up @@ -64,30 +64,6 @@ function isStringTypeInternal(type: ast.AbstractType | ast.TypeDefinition, visit
return false;
}

export function getActionAtElement(element: ast.AbstractElement): ast.Action | undefined {
const parent = element.$container;
if (ast.isGroup(parent)) {
const elements = parent.elements;
const index = elements.indexOf(element);
for (let i = index - 1; i >= 0; i--) {
const item = elements[i];
if (ast.isAction(item)) {
return item;
} else {
const action = streamAllContents(elements[i]).find(ast.isAction);
if (action) {
return action;
}
}
}
}
if (ast.isAbstractElement(parent)) {
return getActionAtElement(parent);
} else {
return undefined;
}
}

export function getTypeNameWithoutError(type?: ast.AbstractType | ast.InferredType): string | undefined {
if (!type) {
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/langium/src/grammar/references/grammar-references.ts
Expand Up @@ -14,11 +14,11 @@ 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 } from '../../utils/grammar-util.js';
import { findAssignment, findNodeForProperty, getActionAtElement } from '../../utils/grammar-util.js';
import { stream } from '../../utils/stream.js';
import { UriUtils } from '../../utils/uri-util.js';
import { isAction, isAssignment, isInterface, isParserRule, isType, isTypeAttribute } from '../generated/ast.js';
import { extractAssignments, getActionAtElement } from '../internal-grammar-util.js';
import { extractAssignments } from '../internal-grammar-util.js';
import { collectChildrenTypes, collectSuperTypes } from '../type-system/types-util.js';

export class LangiumGrammarReferences extends DefaultReferences {
Expand Down
24 changes: 24 additions & 0 deletions packages/langium/src/utils/grammar-util.ts
Expand Up @@ -269,6 +269,30 @@ function findNameAssignmentInternal(type: ast.AbstractType, cache: Map<ast.Abstr
return undefined;
}

export function getActionAtElement(element: ast.AbstractElement): ast.Action | undefined {
const parent = element.$container;
if (ast.isGroup(parent)) {
const elements = parent.elements;
const index = elements.indexOf(element);
for (let i = index - 1; i >= 0; i--) {
const item = elements[i];
if (ast.isAction(item)) {
return item;
} else {
const action = streamAllContents(elements[i]).find(ast.isAction);
if (action) {
return action;
}
}
}
}
if (ast.isAbstractElement(parent)) {
return getActionAtElement(parent);
} else {
return undefined;
}
}

export type Cardinality = '?' | '*' | '+' | undefined;
export type Operator = '=' | '+=' | '?=' | undefined;

Expand Down
3 changes: 2 additions & 1 deletion packages/langium/test/grammar/lsp/grammar-formatter.test.ts
Expand Up @@ -5,7 +5,8 @@
******************************************************************************/

import { describe, test } from 'vitest';
import { EmptyFileSystem, createLangiumGrammarServices, expandToString } from 'langium';
import { EmptyFileSystem, expandToString } from 'langium';
import { createLangiumGrammarServices } from 'langium/grammar';
import { expectFormatting } from 'langium/test';

const services = createLangiumGrammarServices(EmptyFileSystem);
Expand Down
5 changes: 2 additions & 3 deletions packages/langium/test/parser/langium-parser-builder.test.ts
Expand Up @@ -6,10 +6,9 @@

import type { TokenType, TokenVocabulary } from 'chevrotain';
import type { AstNode, Grammar, GrammarAST, LangiumParser, TokenBuilderOptions } from 'langium';
import { createLangiumGrammarServices, EmptyFileSystem, createServicesForGrammar, DefaultTokenBuilder} from 'langium';
import { EmptyFileSystem, DefaultTokenBuilder } from 'langium';
import { describe, expect, test, onTestFailed, beforeEach } from 'vitest';
import { DefaultTokenBuilder } from 'langium';
import { createServicesForGrammar } from 'langium/grammar';
import { createLangiumGrammarServices, createServicesForGrammar } from 'langium/grammar';
import { parseHelper } from 'langium/test';
import { EOF } from 'chevrotain';

Expand Down

0 comments on commit 9d6a154

Please sign in to comment.