Skip to content

Commit

Permalink
feat: refactor editor tokenlize #24
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Mar 17, 2024
1 parent 6bc35e9 commit 8f6a80e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 107 deletions.
50 changes: 50 additions & 0 deletions src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ export type Decoration = {
range: Range;
options: { isWholeLine: boolean; linesDecorationsClassName: string };
};

export const executeActions = {
regexp: /^(GET|DELETE|POST|PUT)\s\w+/,
decorationClassName: 'action-execute-decoration',
};

export type SearchToken = {
qdsl: string;
actionPosition: Range;
qdslPosition: Range;
method: string;
index: string;
path: string;
};

export const searchTokensProvider = {
// Set defaultToken to invalid to see what you do not tokenize yet
defaultToken: 'invalid',
Expand Down Expand Up @@ -193,3 +204,42 @@ export const searchTokensProvider = {
],
},
};

export const buildSearchToken = (lines: Array<{ lineNumber: number; lineContent: string }>) => {
const commands = lines.filter(({ lineContent }) => executeActions.regexp.test(lineContent));

return commands.map(({ lineContent, lineNumber }, index, commands) => {
const rawCmd = lineContent.split(/[/\s]+/);
const method = rawCmd[0]?.toUpperCase();
const indexName = rawCmd[1]?.startsWith('_') ? undefined : rawCmd[1];
const path = rawCmd.slice(indexName ? 2 : 1, rawCmd.length).join('/');
const endLineNumber = commands[index + 1]?.lineNumber
? commands[index + 1]?.lineNumber - 1
: lines.length;

const qdsl = lines
.slice(lineNumber, endLineNumber)
.map(({ lineContent }) => lineContent)
.join('');
return {
qdsl,
method,
index: indexName,
path,
actionPosition: {
startLineNumber: lineNumber,
endLineNumber: lineNumber,
startColumn: 1,
endColumn: lineContent.length,
},
qdslPosition: qdsl
? {
startLineNumber: lineNumber + 1,
startColumn: 1,
endLineNumber: endLineNumber,
endColumn: lines[endLineNumber - 1].lineContent.length,
}
: null,
} as SearchToken;
});
};
7 changes: 5 additions & 2 deletions src/layout/components/the-aside.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
</template>

<script setup lang="ts">
import { ref, markRaw } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { DataBase, Folders, LogoGithub, Settings, UserAvatar, ExpandAll } from '@vicons/carbon';
import { useAppStore } from './../../store';
import { useAppStore } from '../../store';
import theAsideIcon from './the-aside-icon.vue';
const router = useRouter();
const route = useRoute();
const appStore = useAppStore();
const { setConnectPanel } = appStore;
const mainNavList = ref([
{
Expand Down Expand Up @@ -113,7 +116,7 @@ const navClick = (item: RouteItem) => {
window.electronAPI.openGitHub();
} else {
if (route.path === item.path) {
appStore.setConnectPannel();
setConnectPanel();
} else {
router.push({
path: item.path,
Expand Down
8 changes: 6 additions & 2 deletions src/store/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export enum LanguageType {
EN_US = 'enUS',
}
export const useAppStore = defineStore('app', {
state: () => {
state: (): {
themeType: ThemeType;
languageType: LanguageType;
connectPanel: boolean;
} => {
return {
themeType: ThemeType.AUTO,
languageType: LanguageType.AUTO,
Expand All @@ -19,7 +23,7 @@ export const useAppStore = defineStore('app', {
},
persist: true,
actions: {
setConnectPannel() {
setConnectPanel() {
this.connectPanel = !this.connectPanel;
},
getEditorTheme() {
Expand Down
12 changes: 7 additions & 5 deletions src/store/connectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const buildPath = (index: string | undefined, path: string | undefined) => {
};

export const useConnectionStore = defineStore('connectionStore', {
state(): {
state: (): {
connections: Connection[];
established: Established;
} {
} => {
return {
connections: [],
established: null,
Expand All @@ -69,7 +69,7 @@ export const useConnectionStore = defineStore('connectionStore', {
return await client.get(undefined, 'format=json');
},
saveConnection(connection: Connection) {
const index = this.connections.findIndex(item => item.id === connection.id);
const index = this.connections.findIndex(({ id }: Connection) => id === connection.id);
if (index >= 0) {
this.connections[index] = connection;
} else {
Expand All @@ -78,7 +78,7 @@ export const useConnectionStore = defineStore('connectionStore', {
storeAPI.set('connections', pureObject(this.connections));
},
removeConnection(connection: Connection) {
this.connections = this.connections.filter(item => item.id !== connection.id);
this.connections = this.connections.filter(({ id }: Connection) => id !== connection.id);
storeAPI.set('connections', pureObject(this.connections));
},
async establishConnection(connection: Connection) {
Expand Down Expand Up @@ -112,7 +112,9 @@ export const useConnectionStore = defineStore('connectionStore', {
selectIndex(indexName: string) {
this.established = {
...this.established,
activeIndex: this.established?.indices.find(({ index }) => index === indexName),
activeIndex: this.established?.indices.find(
({ index }: { index: string }) => index === indexName,
),
} as Established;
},
async searchQDSL({
Expand Down
14 changes: 8 additions & 6 deletions src/views/connect/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="connect-container">
<div v-if="isPannelOpen" class="connect-list">
<div v-if="connectPanel" class="connect-list">
<div class="add-connect" @click="addConnect">
<n-icon size="28">
<Add />
Expand All @@ -22,6 +22,8 @@
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { storeToRefs } from 'pinia';
import { Add } from '@vicons/carbon';
import ConnectModal from './components/connect-dialog.vue';
import connectList from './components/connect-list.vue';
Expand All @@ -30,15 +32,15 @@ import Editor from '../editor/index.vue';
import { useAppStore } from '../../store';
const appStore = useAppStore();
const { setConnectPanel } = appStore;
const { connectPanel } = storeToRefs(appStore);
// DOM
const connectModalRef = ref();
const isPannelOpen = computed(() => {
return appStore.connectPannel;
});
onMounted(() => {
if (!appStore.connectPannel) {
appStore.setConnectPannel();
if (!connectPanel.value) {
setConnectPanel();
}
});
Expand Down
Loading

0 comments on commit 8f6a80e

Please sign in to comment.