Skip to content

Commit

Permalink
refactor: change memo logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoKevin committed Jul 30, 2024
1 parent 851b8d7 commit b596105
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { jumpTo } from './jump';
import { JumpType, getSymbolType, memoScopeLanguages, type VueSymbolType } from './types';
import { debounce } from './util';

export type DocumentFocusCache = Map<VueSymbolType, Position>;
export type FocusScopeMemo = Map<VueSymbolType, Position>;

export function activate(context: ExtensionContext) {
const focusPositionCache = new Map<string, DocumentFocusCache>();
const focusPositionCache = new Map<string, FocusScopeMemo>();

const disposables = Object.values(JumpType).map((type) =>
commands.registerTextEditorCommand(type, (textEditor) => {
Expand Down
24 changes: 13 additions & 11 deletions src/jump.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Position, Range, Selection, TextEditorRevealType, type TextEditor } from 'vscode';
import type { DocumentFocusCache } from '.';
import type { FocusScopeMemo } from '.';
import { findInsertImportPosition, findSymbolPosition } from './positionHelper';
import { JumpType, JumpTypeSymbolMap, type Nullable } from './types';
import { JumpType, memoScopeSymbolMap, type Nullable } from './types';

export async function jumpTo(target: JumpType, textEditor: TextEditor, cache?: DocumentFocusCache) {
export async function jumpTo(jumpType: JumpType, textEditor: TextEditor, cache?: FocusScopeMemo) {
let targetPosition: Nullable<Position>;

if (textEditor.document.languageId === 'vue') {
if (
target === JumpType.ScriptStart ||
target === JumpType.TemplateStart ||
target === JumpType.StyleStart
jumpType === JumpType.ScriptStart ||
jumpType === JumpType.TemplateStart ||
jumpType === JumpType.StyleStart
) {
targetPosition = await findSymbolPosition(textEditor, target);
} else if (target === JumpType.ScriptImports) {
targetPosition = await findSymbolPosition(textEditor, jumpType);
} else if (jumpType === JumpType.ScriptImports) {
targetPosition = await findSymbolPosition(textEditor, JumpType.ScriptStart);
if (targetPosition) {
const position = await findInsertImportPosition(textEditor.document, true);
Expand All @@ -22,14 +22,16 @@ export async function jumpTo(target: JumpType, textEditor: TextEditor, cache?: D
}
}
} else {
const position = cache?.get(JumpTypeSymbolMap[target]);
const position = memoScopeSymbolMap[jumpType]
? cache?.get(memoScopeSymbolMap[jumpType])
: null;
if (position) {
targetPosition = position;
} else {
targetPosition = await findSymbolPosition(textEditor, target);
targetPosition = await findSymbolPosition(textEditor, jumpType);
}
}
} else {
} else if (jumpType === JumpType.ScriptImports) {
targetPosition = await findInsertImportPosition(textEditor.document, false);
}

Expand Down
4 changes: 2 additions & 2 deletions src/positionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type TextDocument,
type TextEditor,
} from 'vscode';
import { getSymbolType, JumpTypeSymbolMap, type JumpType, type Nullable } from './types';
import { getSymbolType, memoScopeSymbolMap, type JumpType, type Nullable } from './types';

export async function findInsertImportPosition(document: TextDocument, isVue: boolean) {
let importPosition: Nullable<Position> = new Position(0, 0);
Expand Down Expand Up @@ -63,5 +63,5 @@ export async function findSymbolPosition(textEditor: TextEditor, target: JumpTyp
'vscode.executeDocumentSymbolProvider',
textEditor.document.uri
);
return symbols.find((s) => getSymbolType(s) === JumpTypeSymbolMap[target])?.range?.start;
return symbols.find((s) => getSymbolType(s) === memoScopeSymbolMap[target])?.range?.start;
}
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ export function getSymbolType(symbol: DocumentSymbol) {
return Object.values(VueSymbolType).find((v) => symbol.name.startsWith(v));
}

export const JumpTypeSymbolMap = {
export const memoScopeSymbolMap: Partial<Record<JumpType, VueSymbolType>> = {
[JumpType.ScriptFocus]: VueSymbolType.Script,
[JumpType.ScriptStart]: VueSymbolType.Script,
[JumpType.ScriptImports]: VueSymbolType.Script,
[JumpType.TemplateFocus]: VueSymbolType.Template,
[JumpType.TemplateStart]: VueSymbolType.Template,
[JumpType.StyleFocus]: VueSymbolType.Style,
[JumpType.StyleStart]: VueSymbolType.Style,
};

export const memoScopeLanguages = ['vue', 'svelte'];
export const memoScopeLanguages = ['vue'];

export type Nullable<T> = T | null | undefined;

0 comments on commit b596105

Please sign in to comment.