Skip to content

Commit

Permalink
Enable component name autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBonnemaison committed Jul 5, 2020
1 parent 2f2e96a commit 8e5c21d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/webapp/features/filters/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { all, call, put, select, takeLatest } from 'redux-saga/effects';

import { actions } from './slice';
import { actions as appActions } from '../app/slice';
import { actions as studioActions } from '../studio/slice';
import { selectors as studioSelectors } from '../studio/slice';

import { architectureResourceApi, basePath } from '../../services/config';
Expand Down Expand Up @@ -131,6 +132,7 @@ function* refresh() {
export default function* root() {
yield all([
takeLatest(actions.getInitial, getInitial),
takeLatest(studioActions.setRaw, getInitial),
takeLatest(appActions.setSource, refresh)
]);
}
29 changes: 25 additions & 4 deletions src/main/webapp/features/studio/Editor/editorOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as monaco from 'monaco-editor';

import { architectureResourceApi } from '../../../services/config';
import { selectors as filtersSelectors } from '../../filters/slice';
import { store } from '../../../store';

monaco.editor.defineTheme('lowfer', {
base: 'vs', // can also be vs-dark or hc-black
Expand All @@ -23,26 +25,45 @@ const createComponentTypesProposals = async (range: any) => {
}));
};

const createComponentNamesProposals = (range: any) => {
const components = filtersSelectors.getComponents(store.getState());
return components?.map((component) => ({
label: component.name,
kind: monaco.languages.CompletionItemKind.Function,
documentation: component.name,
insertText: component.name,
range
}));
};

monaco.languages.registerCompletionItemProvider('yaml', {
triggerCharacters: [':', ' '],
// @ts-ignore
provideCompletionItems: (model, position) => {
const textUntilPosition = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column
});
const match = textUntilPosition.match(/type:/);
if (!match) {
return { suggestions: [] };
}
const word = model.getWordUntilPosition(position);
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn
};
const namesMatch = textUntilPosition.match(/component:/);
if (namesMatch) {
return {
suggestions: createComponentNamesProposals(range)
};
}
const typesMatch = textUntilPosition.match(/type:/);
if (!typesMatch) {
return { suggestions: [] };
}

return new Promise((resolve) => {
createComponentTypesProposals(range).then((suggestions) => {
resolve({
Expand Down
4 changes: 0 additions & 4 deletions src/main/webapp/features/studio/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const Editor = () => {

const [value, setValue] = useState(raw);

useEffect(() => {
dispatch(actions.getComponentTypes());
}, [dispatch]);

useEffect(() => {
setValue(raw);
}, [setValue, raw]);
Expand Down

0 comments on commit 8e5c21d

Please sign in to comment.