Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorize & autocomplete #114

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dmn-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@kie-tools-core/patternfly-base": "workspace:*",
"@kie-tools-core/switch-expression-ts": "workspace:*",
"@kie-tools/boxed-expression-component": "workspace:*",
"@kie-tools/dmn-feel-antlr4-parser": "workspace:*",
"@kie-tools/dmn-marshaller": "workspace:*",
"@kie-tools/feel-input-component": "workspace:*",
"@kie-tools/i18n-common-dictionary": "workspace:*",
Expand Down
15 changes: 14 additions & 1 deletion packages/dmn-editor/src/boxedExpressions/BoxedExpression.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,27 @@ import {
} from "@kie-tools/pmml-editor-marshaller/dist/marshaller/model/pmml4_4";
import { PMMLFieldData } from "@kie-tools/pmml-editor-marshaller/dist/api/PMMLFieldData";
import { getDefaultColumnWidth } from "./getDefaultColumnWidth";
import { FeelVariables } from "@kie-tools/dmn-feel-antlr4-parser";
import { DmnLatestModel } from "@kie-tools/dmn-marshaller";

export function BoxedExpression({ container }: { container: React.RefObject<HTMLElement> }) {
const thisDmn = useDmnEditorStore((s) => s.dmn);
const diagram = useDmnEditorStore((s) => s.diagram);
const dispatch = useDmnEditorStore((s) => s.dispatch);
const boxedExpressionEditor = useDmnEditorStore((s) => s.boxedExpressionEditor);

const { externalDmnsByNamespace } = useDmnEditorDerivedStore();
const dmnEditorStoreApi = useDmnEditorStoreApi();

const feelVariables = useMemo(() => {
const externalModels = new Map<string, DmnLatestModel>();

for (const [key, externalDmn] of externalDmnsByNamespace) {
externalModels.set(key, externalDmn.model);
}

return new FeelVariables(thisDmn.model.definitions, externalModels);
}, [externalDmnsByNamespace, thisDmn.model.definitions]);

const widthsById = useMemo(() => {
return (
thisDmn.model.definitions["dmndi:DMNDI"]?.["dmndi:DMNDiagram"]?.[diagram.drdIndex]["di:extension"]?.[
Expand Down Expand Up @@ -307,6 +319,7 @@ export function BoxedExpression({ container }: { container: React.RefObject<HTML
setExpressionDefinition={setExpression}
dataTypes={dataTypes}
scrollableParentRef={container}
variables={feelVariables}
/>
</div>
)}
Expand Down
17 changes: 14 additions & 3 deletions packages/dmn-feel-antlr4-parser/src/FeelVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@
*/

import { FeelVariablesParser } from "./parser/FeelVariablesParser";
import { VariablesRepository } from "./parser/VariablesRepository";
import { DmnDefinitions, VariablesRepository } from "./parser/VariablesRepository";
import { DmnLatestModel, getMarshaller } from "@kie-tools/dmn-marshaller";

export class FeelVariables {
private readonly _parser: FeelVariablesParser;
private readonly _repository: VariablesRepository;

constructor(xml: string) {
this._repository = new VariablesRepository(xml);
constructor(dmnDefinitions: DmnDefinitions, externalDefinitions: Map<string, DmnLatestModel>) {
this._repository = new VariablesRepository(dmnDefinitions, externalDefinitions);
this._parser = new FeelVariablesParser(this._repository);
}

static fromModelXml(xml: string): FeelVariables {
const def = this.getDefinitions(xml);
return new FeelVariables(def, new Map<string, DmnLatestModel>());
}

static getDefinitions(xml: string) {
const marshaller = getMarshaller(xml, { upgradeTo: "latest" });
return marshaller.parser.parse().definitions;
}

get parser(): FeelVariablesParser {
return this._parser;
}
Expand Down
16 changes: 14 additions & 2 deletions packages/dmn-feel-antlr4-parser/src/parser/FeelVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,34 @@

import { FeelSyntacticSymbolNature } from "./FeelSyntacticSymbolNature";
import { FeelSymbol } from "./FeelSymbol";
import { Variable } from "./Variable";

export class FeelVariable {
private readonly _text: string;
private readonly _startIndex: number;
private _startIndex: number;
private readonly _feelSymbolNature: FeelSyntacticSymbolNature;
private readonly _length: number;
private readonly _scopeSymbols: FeelSymbol[];
private readonly _source: Variable | undefined;

constructor(
startIndex: number,
length: number,
symbolType: FeelSyntacticSymbolNature,
text: string,
scopeSymbols?: FeelSymbol[]
scopeSymbols?: FeelSymbol[],
source?: Variable
) {
this._startIndex = startIndex;
this._length = length;
this._feelSymbolNature = symbolType;
this._text = text;
this._scopeSymbols = scopeSymbols ?? [];
this._source = source;
}

get source(): Variable | undefined {
return this._source;
}

get text(): string {
Expand All @@ -49,6 +57,10 @@ export class FeelVariable {
return this._startIndex;
}

set startIndex(value: number) {
this._startIndex = value;
}

get feelSymbolNature(): FeelSyntacticSymbolNature {
return this._feelSymbolNature;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export class FeelVariablesParser {

constructor(variablesSource: VariablesRepository) {
this.variablesRepository = variablesSource;
this.refreshExpressions();
}

public refreshExpressions() {
for (const expression of this.variablesRepository.expressions.values()) {
for (const variable of expression.variables) {
variable.source?.expressions.delete(expression.uuid);
}
const parsedExpression = this.parse(expression.uuid, expression.fullExpression);
expression.variables = parsedExpression.feelVariables;
for (const variable of parsedExpression.feelVariables) {
variable.source?.expressions.set(expression.uuid, expression);
}
}
}

public parse(variableContextUuid: string, expression: string): ParsedExpression {
Expand Down Expand Up @@ -117,7 +131,8 @@ export class FeelVariablesParser {
parser.helper.defineVariable(
context.variable.value,
context.variable.typeRef ? this.createType(context.variable.typeRef) : undefined,
context.variable.feelSyntacticSymbolNature
context.variable.feelSyntacticSymbolNature,
context.variable
);
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/dmn-feel-antlr4-parser/src/parser/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { DataType } from "./DataType";
import { FeelSyntacticSymbolNature } from "./FeelSyntacticSymbolNature";
import { Expression } from "./VariableOccurrence";

/**
* Describe a variable in FEEL.
Expand All @@ -38,4 +39,9 @@ export interface Variable {
* The type of the variable, which can be a custom data type defined by the user, a built-in type or not defined.
*/
typeRef?: DataType | string | undefined;

/**
* The expressions where this variable is being used.
*/
expressions: Map<string, Expression>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Variable } from "./Variable";
import { FeelVariable } from "./FeelVariable";

export class Expression {
private readonly _uuid: string;
private _fullExpression: string;
private _variables: Array<FeelVariable>;

constructor(uuid: string, fullExpression?: string) {
this._uuid = uuid;
this._variables = new Array<FeelVariable>();
this._fullExpression = fullExpression ?? "";
}

public renameVariable(renamedVariable: Variable, newName: String) {
// We assume that variables are already ordered by the parser

let offset = 0;
for (const variable of this._variables) {
variable.startIndex += offset;
if (variable.source != undefined && variable.source === renamedVariable) {
this.replaceAt(variable.startIndex, renamedVariable.value.length, newName);
offset += renamedVariable.value.length - newName.length;
}
}
}

private replaceAt(position: number, oldLength: number, newVariable: String) {
const part1 = this.fullExpression.substring(0, position);
const newPart = newVariable;
const part2 = this.fullExpression.substring(position + oldLength);

this.fullExpression = part1 + newPart + part2;
}

get variables(): Array<FeelVariable> {
return this._variables;
}

set variables(value: Array<FeelVariable>) {
this._variables = value;
}

get fullExpression(): string {
return this._fullExpression;
}

set fullExpression(value: string) {
this._fullExpression = value;
}

get uuid(): string {
return this._uuid;
}
}
Loading
Loading