Skip to content

Commit

Permalink
feat: refactor #10
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Jan 6, 2024
1 parent 4a1ceb0 commit c97d6ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/common/searchTokensProvider.ts → src/common/editor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
type Range = {
startLineNumber: number;
startColumn: number;
endLineNumber: number;
endColumn: number;
};
export type Decoration = {
id: number;
range: Range;
options: { isWholeLine: boolean; linesDecorationsClassName: string };
};

export const searchTokensProvider = {
// Set defaultToken to invalid to see what you do not tokenize yet
defaultToken: 'invalid',
Expand Down
2 changes: 1 addition & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './customError';
export * from './debounceThrottle';
export * from './debug';
export * from './pureObject';
export * from './searchTokensProvider';
export * from './editor';
19 changes: 8 additions & 11 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<script setup lang="ts">
import * as monaco from 'monaco-editor';
import { storeToRefs } from 'pinia';
import { CustomError, searchTokensProvider } from '../../common';
import { CustomError, Decoration, searchTokensProvider } from '../../common';
import { useAppStore, useSourceFileStore, useConnectionStore } from '../../store';
import { useLang } from '../../lang';
type Editor = ReturnType<typeof monaco.editor.create>;
Expand Down Expand Up @@ -91,15 +91,10 @@ watch(
const executionGutterClass = 'execute-button-decoration';
const executeDecorationType = 'action-execute-decoration.search';
type Decoration = {
id: number;
range: monaco.Range;
options: { isWholeLine: boolean; linesDecorationsClassName: string };
};
let executeDecorations: Array<Decoration> = [];
const getActionMarksDecorations = (editor: monaco.Editor): Array<Decoration> => {
const getActionMarksDecorations = (editor: Editor): Array<Decoration> => {
// Get the model of the editor
const model = editor.getModel();
// Tokenize the entire content of the model
Expand All @@ -117,12 +112,12 @@ const getActionMarksDecorations = (editor: monaco.Editor): Array<Decoration> =>
.sort((a, b) => (a as Decoration).id - (b as Decoration).id) as Array<Decoration>;
};
const refreshActionMarks = (editor: monaco.Editor) => {
const refreshActionMarks = (editor: Editor) => {
const freshedDecorations = getActionMarksDecorations(editor);
// @See https://github.com/Microsoft/monaco-editor/issues/913#issuecomment-396537569
executeDecorations = editor.deltaDecorations(executeDecorations, freshedDecorations);

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Type 'string[]' is not assignable to type 'Decoration[]'.

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

Argument of type 'Decoration[]' is not assignable to parameter of type 'string[]'.

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Type 'string[]' is not assignable to type 'Decoration[]'.

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Argument of type 'Decoration[]' is not assignable to parameter of type 'string[]'.

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

Type 'string[]' is not assignable to type 'Decoration[]'.

Check failure on line 118 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

Argument of type 'Decoration[]' is not assignable to parameter of type 'string[]'.
};
const getAction = (editor: monaco.Editor, startLine: number) => {
const getAction = (editor: Editor, startLine: number) => {
const model = editor.getModel();
const action = model.getLineContent(startLine);

Check failure on line 122 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'model' is possibly 'null'.

Check failure on line 122 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'model' is possibly 'null'.

Check failure on line 122 in src/views/editor/index.vue

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'model' is possibly 'null'.
Expand All @@ -143,8 +138,8 @@ const getAction = (editor: monaco.Editor, startLine: number) => {
};
const executeQueryAction = async (
queryEditor: monaco.Editor,
displayEditor: monaco.Editor,
queryEditor: Editor,
displayEditor: Editor,
position: { column: number; lineNumber: number },
) => {
const { action, payload } = getAction(queryEditor, position.lineNumber);
Expand All @@ -171,13 +166,15 @@ const executeQueryAction = async (
});
}
};
const setupQueryEditor = (code: string) => {
queryEditor = monaco.editor.create(queryEditorRef.value, {
automaticLayout: true,
theme: editorTheme.value,
value: code,
language: 'search',
});
// Register language injection rule
queryEditor.onKeyUp(event => refreshActionMarks(queryEditor));
queryEditor.onMouseDown(({ event, target }) => {
Expand Down

0 comments on commit c97d6ad

Please sign in to comment.