Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoLaval committed Oct 10, 2023
1 parent 736c78b commit 9cf1533
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 1,644 deletions.
1,488 changes: 0 additions & 1,488 deletions samples/SDMXInputFiles.json

This file was deleted.

57 changes: 31 additions & 26 deletions src/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { useState, useRef, useCallback } from "react";
import { useState, useEffect, useRef, useCallback } from "react";
import MonacoEditor from "@monaco-editor/react";
import * as Monaco from "monaco-editor/esm/vs/editor/editor.api";
import { validate } from "./utils/ParserFacade";
import { getEditorWillMount } from "./utils/providers";
import { Tools, Error, CursorPosition, Variables, SdmxResult } from "./model";
import { buildVariables } from "./utils/variables";
import { Tools, Error, Variables } from "./model";
import { buildVariables, buildUniqueVariables } from "./utils/variables";

type EditorProps = {
script?: string;
setScript?: (value: string) => void;
// customFetcher?: (url: string) => Promise<any>;
sdmxResult?: SdmxResult;
// sdmxResultURL?: string;
// readOnly?: boolean;
customFetcher?: (url: string) => Promise<any>;
variables?: Variables;
// variableURLs: string[];
variablesInputURLs?: string[];
SDMXInputURLs?: string[];
tools: Tools;
// options: Options;
onCursorChange?: (position: CursorPosition) => void;
onListErrors?: (errors: Error[]) => void;
height?: string;
width?: string;
Expand All @@ -29,9 +25,10 @@ const Editor = ({
script,
setScript,
onListErrors,
onCursorChange,
customFetcher,
variables,
sdmxResult,
variablesInputURLs,
SDMXInputURLs,
tools,
height = "50vh",
width = "100%",
Expand All @@ -40,8 +37,8 @@ const Editor = ({
}: EditorProps) => {
const editorRef = useRef<Monaco.editor.IStandaloneCodeEditor | null>(null);
const monacoRef = useRef<typeof Monaco | null>(null);
const [vars] = useState(buildVariables(variables));
const [sdmxRes] = useState(sdmxResult);
const [ready, setReady] = useState<boolean>(false);
const [vars, setVars] = useState(buildVariables(variables));

const onMount = (editor: Monaco.editor.IStandaloneCodeEditor, mon: typeof Monaco, t: Tools) => {
editorRef.current = editor;
Expand All @@ -62,17 +59,6 @@ const Editor = ({
}
}
});

editor.onDidChangeCursorPosition(() => {
if (onCursorChange) {
const position = editor.getPosition();
if (position)
onCursorChange({
line: position.lineNumber,
column: position.column
});
}
});
};

const parseContent = useCallback(
Expand Down Expand Up @@ -107,6 +93,26 @@ const Editor = ({
[onListErrors]
);

useEffect(() => {
if (!Array.isArray(variablesInputURLs) || variablesInputURLs.length === 0) setReady(true);
const f = customFetcher || fetch;
if (variablesInputURLs && variablesInputURLs.length > 0 && !ready) {
Promise.all(variablesInputURLs.map(v => f(v)))
.then(res =>
Promise.all(res.map(r => r.json())).then(res => {
const uniqueVars = buildUniqueVariables(res)
setVars(v => [...v, ...uniqueVars]);
setReady(true);
})
)
.catch(() => {
setReady(true);
});
}
}, [variablesInputURLs, SDMXInputURLs]);

if (!ready) return null;

return (
<MonacoEditor
value={script}
Expand All @@ -117,7 +123,6 @@ const Editor = ({
onMount(e, m, tools);
getEditorWillMount(tools)({
variables: vars,
sdmxResult: sdmxRes,
editor: e
})(m);
}}
Expand Down
4 changes: 0 additions & 4 deletions src/model/cursor-position.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export * from "./codeList";
export * from "./cursor-position";
export * from "./dataStructureDefinition";
export * from "./error";
export * from "./options";
export * from "./sdmxResult";
export * from "./tools";
export * from "./variables";
6 changes: 0 additions & 6 deletions src/model/options.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/model/sdmxResult.ts

This file was deleted.

11 changes: 9 additions & 2 deletions src/stories/VtlEditor.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ const variables = {
};

export const Enriched = {
args: { variables },
args: {
variables,
variablesInputURLs: [
"https://raw.githubusercontent.com/Making-Sense-Info/ANTLR-Editor/gh-pages/samples/variablesInputFile1.json",
"https://raw.githubusercontent.com/Making-Sense-Info/ANTLR-Editor/gh-pages/samples/variablesInputFile2.json"
]
},
argTypes: {
variables: { control: "object" }
variables: { control: "object" },
variablesInputURLs: { control: "object" }
}
};
54 changes: 0 additions & 54 deletions src/utils/completionItemMapper.ts

This file was deleted.

50 changes: 5 additions & 45 deletions src/utils/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { createLexer, createParser } from "./ParserFacade";
import { TokensProvider } from "./tokensProvider";
import { VocabularyPack } from "./vocabularyPack";
import { VARIABLE } from "./constants";
import { Tools, SdmxResult, Variable } from "../model";
import { fromISdmxResult } from "./completionItemMapper";
import { Tools, Variable } from "../model";

export const getTheme = (): EditorApi.editor.IStandaloneThemeData => {
return {
Expand Down Expand Up @@ -49,15 +48,7 @@ let completionItemDispose: IDisposable | undefined = undefined;

export const getEditorWillMount =
(tools: Tools) =>
({
variables,
sdmxResult,
editor
}: {
variables: Variable[];
sdmxResult: SdmxResult | undefined;
editor: Monaco.editor.IStandaloneCodeEditor;
}) => {
({ variables, editor }: { variables: Variable[]; editor: Monaco.editor.IStandaloneCodeEditor }) => {
const { id } = tools;
return (monaco: typeof EditorApi) => {
monaco.languages.register({ id });
Expand All @@ -74,8 +65,7 @@ export const getEditorWillMount =
}
completionItemDispose = monaco.languages.registerCompletionItemProvider(id, {
provideCompletionItems: getSuggestions(tools, {
variables,
sdmxResult
variables
})
});

Expand Down Expand Up @@ -163,30 +153,7 @@ export const getVtlTheme = (name: string): EditorApi.editor.IStandaloneThemeData
return { base: "vs", colors: {}, inherit: true, rules: [] };
};

function removeItemsFromList<T>(items: T[], list: T[]): T[] {
return list.filter(val => !items.includes(val));
}

const removeCodeListsFromList = (importedDsd: SdmxResult, vars: string[]) => {
const codeListsId: string[] = importedDsd.dimension.codeLists
.concat(importedDsd.attribute.codeLists)
.map(cl => cl.structureId);
const textsId: string[] = importedDsd.dimension.texts
.concat(importedDsd.attribute.texts)
.map(cl => cl.id);
const listToRemove = [
...codeListsId,
...textsId,
importedDsd.timeDimension,
importedDsd.primaryMeasure
];
return removeItemsFromList(listToRemove, vars);
};

const getSuggestions = (
tools: Tools,
{ variables, sdmxResult }: { variables?: Variable[]; sdmxResult?: SdmxResult }
): any => {
const getSuggestions = (tools: Tools, { variables }: { variables?: Variable[] }): any => {
return function (model: editor.ITextModel, position: Position) {
const textUntilPosition = model.getValueInRange({
startLineNumber: 1,
Expand Down Expand Up @@ -220,13 +187,6 @@ const getSuggestions = (
: buildGrammarGraph(tools).suggestions();
uniquetext = removeLanguageSyntaxFromList(uniquetext, suggestionList);

let mappedCodeLists: languages.CompletionItem[] = [];

if (sdmxResult) {
uniquetext = removeCodeListsFromList(sdmxResult, uniquetext);
mappedCodeLists = fromISdmxResult(sdmxResult, range);
}

const array = uniquetext.map(w => {
return {
label: w,
Expand All @@ -241,7 +201,7 @@ const getSuggestions = (
range
}));
return {
suggestions: [...array, ...vars, ...suggestionList, ...mappedCodeLists]
suggestions: [...array, ...vars, ...suggestionList]
};
};

Expand Down

0 comments on commit 9cf1533

Please sign in to comment.