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

feat(autocompletion): support nested variables #34

Merged
merged 7 commits into from
Dec 7, 2022
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
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const editor = new FeelEditor({

### Variables

You can provide a variables array that will be used for auto completion. The Variables
need to be in the following format:
You can provide a variables array that will be used for auto completion. Nested
structures are supported.
The Variables need to be in the following format:

```JavaScript
const editor = new FeelEditor({
Expand All @@ -38,7 +39,13 @@ const editor = new FeelEditor({
{
name: 'variablename to match',
detail: 'optional inline info',
info: 'optional pop-out info'
info: 'optional pop-out info',
entries: [
{
name: 'nested variable',
...
}
]
}
]
});
Expand All @@ -51,7 +58,7 @@ editor.setVariables([
{
name: 'newName',
detail: 'new variable inline info',
info: 'new pop-out info
info: 'new pop-out info'
}
]);
```
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"mocha": "^10.0.0",
"mocha-test-container-support": "^0.2.0",
"npm-run-all": "^4.1.5",
"puppeteer": "^19.2.2",
"puppeteer": "^19.3.0",
"rollup": "^3.3.0",
"sinon": "^14.0.0",
"sinon-chai": "^3.7.0",
Expand Down
5 changes: 3 additions & 2 deletions src/autocompletion/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const options = tags.map(tag => snippetCompletion(
label: tag.name,
type: 'function',
info: () => {
const html = domify(tag.description);
const html = domify(`<div class="description">${tag.description}<div>`);
return html;
}
},
boost: -1
}
));

Expand Down
4 changes: 3 additions & 1 deletion src/autocompletion/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { autocompletion, completeFromList } from '@codemirror/autocomplete';
import { snippets } from 'lang-feel';
import builtins from './builtins';
import pathExpression from './pathExpressions';

import variables from './variables';

Expand All @@ -10,7 +11,8 @@ export default function() {
override: [
variables,
builtins,
completeFromList(snippets)
completeFromList(snippets.map(s => ({ ...s, boost: -1 }))),
pathExpression
]
})
];
Expand Down
106 changes: 106 additions & 0 deletions src/autocompletion/pathExpressions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { syntaxTree } from '@codemirror/language';
import { isPathExpression } from './autocompletionUtil';
import { variablesFacet } from './VariableFacet';

export default context => {
const variables = context.state.facet(variablesFacet)[0];
const nodeBefore = syntaxTree(context.state).resolve(context.pos, -1);

if (!isPathExpression(nodeBefore)) {
return;
}

const expression = findPathExpression(nodeBefore);

// if the cursor is directly after the `.`, variable starts at the cursor position
const from = nodeBefore === expression ? context.pos : nodeBefore.from;

const path = getPath(expression, context);

let options = variables;
for (var i = 0; i < path.length - 1; i++) {
var childVar = options.find(val => val.name === path[i].name);

if (!childVar) {
return null;
}

// only suggest if variable type matches
if (
childVar.isList !== 'optional' &&
!!childVar.isList !== path[i].isList
) {
return;
}

options = childVar.entries;
}

if (!options) return;

options = options.map(v => ({
label: v.name,
type: 'variable',
info: v.info,
detail: v.detail
}));

const result = {
from: from,
options: options
};

return result;
};


function findPathExpression(node) {
while (node) {
if (node.name === 'PathExpression') {
return node;
}
node = node.parent;
}
}

// parses the path expression into a list of variable names with type information
// e.g. foo[0].bar => [ { name: 'foo', isList: true }, { name: 'bar', isList: false } ]
function getPath(node, context) {
let path = [];

for (let child = node.firstChild; child; child = child.nextSibling) {
if (child.name === 'PathExpression') {
path.push(...getPath(child, context));
} else if (child.name === 'FilterExpression') {
path.push(...getFilter(child, context));
}
else {
path.push({
name: getNodeContent(child, context),
isList: false
});
}
}
return path;
}

function getFilter(node, context) {
const list = node.firstChild;

if (list.name === 'PathExpression') {
const path = getPath(list, context);
const last = path[path.length - 1];
last.isList = true;

return path;
}

return [ {
name: getNodeContent(list, context),
isList: true
} ];
}

function getNodeContent(node, context) {
return context.state.sliceDoc(node.from, node.to);
}
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import theme from './theme';

/**
* @typedef {object} Variable
* @property {string} name
* @property {string} [info]
* @property {string} [detail]
* @property {string} name name or key of the variable
* @property {string} [info] short information about the variable, e.g. type
* @property {string} [detail] longer description of the variable content
* @property {boolean} [isList] whether the variable is a list
* @property {array<Variable>} [schema] array of child variables if the variable is a context or list
*/

const autocompletionConf = new Compartment();
Expand Down
47 changes: 43 additions & 4 deletions test/spec/CodeEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,51 @@ return
{
name: 'Variable1',
info: 'Written in Service Task',
type: 'Process_1'
detail: 'Process_1'
},
{
name: 'Variable2',
info: 'Written in Service Task',
type: 'Process_1'
detail: 'Process_1'
},
{
name: 'ContextVariable',
info: 'This is a Context Variable',
detail: 'Context',
entries: [
{
name: 'child',
info: 'This is a child variable',
detail: 'Context',
entries: [
{
name: 'level2',
info: 'This is a level 2 variable',
detail: 'String'
}
]
}
]
},
{
name: 'ListVariable',
info: 'This is a Context Variable',
detail: 'List',
isList: true,
entries: [
{
name: 'child',
info: 'This is a child variable',
detail: 'Context',
entries: [
{
name: 'level2',
info: 'This is a level 2 variable',
detail: 'String'
}
]
}
]
}
]
});
Expand Down Expand Up @@ -243,12 +282,12 @@ return
{
name: 'Variable1',
info: 'Written in Service Task',
type: 'Process_1'
detail: 'Process_1'
},
{
name: 'Variable2',
info: 'Written in Service Task',
type: 'Process_1'
detail: 'Process_1'
}
]);
}).not.to.throw();
Expand Down
Loading