diff --git a/.eslintrc b/.eslintrc index 09c2e42..1e5ee01 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "@antfu" + "extends": "@antfu", + "rules": { + "no-new":"off" + } } diff --git a/src/CommentFoldingRangeProvider.ts b/src/CommentFoldingRangeProvider.ts index 3b6891e..37c25b2 100644 --- a/src/CommentFoldingRangeProvider.ts +++ b/src/CommentFoldingRangeProvider.ts @@ -8,24 +8,25 @@ import { Ranges } from './getVscodeRange' export class CommentFoldingRangeProvider implements FoldingRangeProvider { provideFoldingRanges(): ProviderResult { - const range = new Ranges() + const { platformInfo } = Ranges + const foldingRanges: FoldingRange[] = [] const startLines = [] const endLines = [] const stack = [] - for (let i = 0; i < range.platformInfo.length; i++) { - const { row, type, line } = range.platformInfo[i] ?? {} + for (let i = 0; i < platformInfo.length; i++) { + const { row, type, line } = platformInfo[i] ?? {} if (type !== 'prefix') continue if (row === '#ifdef' || row === '#ifndef') { - startLines.push(line! - 1) + startLines.push(line - 1) stack.push(startLines.length - 1) } else if (row === '#endif') { const index = stack.pop() if (index !== undefined) - endLines[index] = line! - 1 + endLines[index] = line - 1 } } diff --git a/src/foldOtherPlatformComment.ts b/src/foldOtherPlatformComment.ts index d828353..d562f1f 100644 --- a/src/foldOtherPlatformComment.ts +++ b/src/foldOtherPlatformComment.ts @@ -3,10 +3,11 @@ import { Ranges } from './getVscodeRange' import type { PlatformInfo } from './getPlatformInfo' export async function foldOtherPlatformComment() { - const range = new Ranges() - const { platformList, platformInfo } = range - if (!platformList.length) + const { platformList, platformInfo } = Ranges + if (!platformList.length) { + window.showWarningMessage('该页面没有有效的uni条件编译代码') return + } const platform = await window.showQuickPick([ 'ALL', diff --git a/src/getVscodeRange.ts b/src/getVscodeRange.ts index b15e03d..7270794 100644 --- a/src/getVscodeRange.ts +++ b/src/getVscodeRange.ts @@ -10,9 +10,14 @@ export class Ranges { editor!: TextEditor document!: TextDocument code!: string + + static platformInfo: ReturnType + static platformList: string[] constructor() { this.getContext() + this.setPlatformData() this.hasPlatformList() + this.setColor() } getContext() { @@ -27,26 +32,23 @@ export class Ranges { this.code = document.getText() } - public get platformInfo() { - return getPlatformInfo(this.code) - } - - public get platformList() { - return Array.from(new Set(this.platformInfo.filter(item => item.type === 'platform').map(item => item.row))) - } - - getVscodeRange() { - this.value = transformPlatform(this.platformInfo, this.editor) + setPlatformData() { + Ranges.platformInfo = getPlatformInfo(this.code) + Ranges.platformList = Array.from(new Set(Ranges.platformInfo.filter(item => item.type === 'platform').map(item => item.row))) } hasPlatformList() { - if (this.platformList.length) + if (Ranges.platformList.length) commands.executeCommand('setContext', 'uni.hasComment', true) else commands.executeCommand('setContext', 'uni.hasComment', false) } - public setColor() { + getVscodeRange() { + this.value = transformPlatform(Ranges.platformInfo, this.editor) + } + + setColor() { this.getVscodeRange() setPlatformColor(this.value, this.editor) } diff --git a/src/index.ts b/src/index.ts index 76acbab..8f10e81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,26 +1,16 @@ import type { ExtensionContext } from 'vscode' import { commands, languages, window, workspace } from 'vscode' import { Ranges } from './getVscodeRange' -import { debounce } from './utils' import { CommentFoldingRangeProvider } from './CommentFoldingRangeProvider' import { foldOtherPlatformComment } from './foldOtherPlatformComment' -function onActiveEditorChanged() { - const range = new Ranges() - range.setColor() -} - function setupEventListeners() { - window.onDidChangeActiveTextEditor(onActiveEditorChanged) - workspace.onDidChangeTextDocument(debounce(() => { - const range = new Ranges() - range.setColor() - }, 500)) + window.onDidChangeActiveTextEditor(() => new Ranges()) + workspace.onDidChangeTextDocument(() => new Ranges()) } export function activate(context: ExtensionContext) { - const range = new Ranges() - range.setColor() + new Ranges() setupEventListeners() context.subscriptions.push( @@ -29,8 +19,7 @@ export function activate(context: ExtensionContext) { new CommentFoldingRangeProvider(), ), commands.registerCommand('uni.comment.reload', () => { - const range = new Ranges() - range.setColor() + new Ranges() }), commands.registerCommand('uni.comment.fold-other-platform', () => { foldOtherPlatformComment()