diff --git a/package.json b/package.json index 7f1c65b..67b1687 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ }, { "command": "uni.comment.fold-other-platform", - "title": "Uni Helper: 选择平台并折叠其他平台注释" + "title": "Uni Helper: 选择平台并折叠其他平台注释", + "icon": "$(list-tree)" } ], "configuration": { @@ -55,6 +56,15 @@ "description": "自定义平台高亮" } } + }, + "menus": { + "editor/title": [ + { + "command": "uni.comment.fold-other-platform", + "group": "navigation@0", + "when": "uni.hasComment" + } + ] } }, "scripts": { diff --git a/src/foldOtherPlatformComment.ts b/src/foldOtherPlatformComment.ts index 359e702..49f46e6 100644 --- a/src/foldOtherPlatformComment.ts +++ b/src/foldOtherPlatformComment.ts @@ -1,11 +1,14 @@ import type { ExtensionContext } from 'vscode' import { Position, Range, commands, window } from 'vscode' -import { PLATFORM_LIST } from './constants' import { CommentFoldingRangeProvider } from './CommentFoldingRangeProvider' -export async function foldOtherPlatformComment(_context: ExtensionContext) { +export async function foldOtherPlatformComment( + _context: ExtensionContext, + currentPlatform: string[], +) { const platform = await window.showQuickPick([ - ...PLATFORM_LIST, + 'ALL', + ...currentPlatform, ]) if (!platform) @@ -22,7 +25,7 @@ export async function foldOtherPlatformComment(_context: ExtensionContext) { const text = document.getText(lineNumberToRange(v.start)) return { start: v.start, - flag: !text?.includes(platform), + flag: platform === 'ALL' ? false : !text?.includes(platform), } }) const fold = comments.filter(v => v.flag).map(v => v.start) diff --git a/src/getPlatformInfo.ts b/src/getPlatformInfo.ts index 318684a..35a1ac1 100644 --- a/src/getPlatformInfo.ts +++ b/src/getPlatformInfo.ts @@ -27,6 +27,7 @@ export function getPlatformInfo(code: string): PlatformInfo[] { end, type, color, + row, }) } else if (type === 'platform' && !color) { diff --git a/src/getVscodeRange.ts b/src/getVscodeRange.ts index 6e83afa..eb6c1d6 100644 --- a/src/getVscodeRange.ts +++ b/src/getVscodeRange.ts @@ -1,11 +1,48 @@ -import type { TextEditor } from 'vscode' +import { commands, window } from 'vscode' +import type { TextDocument, TextEditor } from 'vscode' import { getPlatformInfo } from './../src/getPlatformInfo' +import type { HighlightRange } from './transformPlatform' import { transformPlatform } from './transformPlatform' +import { setPlatformColor } from './setPlatformColor' -export function getVscodeRange(editor: TextEditor) { - const code = editor.document.getText() - const platformInfo = getPlatformInfo(code) - const highlightRange = transformPlatform(platformInfo, editor) +export class Ranges { + value!: HighlightRange + editor!: TextEditor + document!: TextDocument + platformList!: string[] + code!: string + constructor() { + this.getContext() + this.getVscodeRange() + this.hasPlatformList() + } - return highlightRange + getContext() { + const editor = window.activeTextEditor + if (!editor) + return + this.editor = editor + const document = editor?.document + if (!document) + return + this.document = document + this.code = document.getText() + } + + getVscodeRange() { + const platformInfo = getPlatformInfo(this.code) + this.platformList = Array.from(new Set(platformInfo.filter(item => item.type === 'platform').map(item => item.row))) + this.value = transformPlatform(platformInfo, this.editor) + } + + hasPlatformList() { + if (this.platformList.length) + commands.executeCommand('setContext', 'uni.hasComment', true) + else + commands.executeCommand('setContext', 'uni.hasComment', false) + } + + setColor() { + setPlatformColor(this.value, this.editor) + } } diff --git a/src/index.ts b/src/index.ts index 6e77a66..0c22358 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,32 +1,26 @@ -import type { ExtensionContext, TextEditor } from 'vscode' +import type { ExtensionContext } from 'vscode' import { commands, languages, window, workspace } from 'vscode' -import { getVscodeRange } from './getVscodeRange' -import { setPlatformColor } from './setPlatformColor' +import { Ranges } from './getVscodeRange' import { debounce } from './utils' import { CommentFoldingRangeProvider } from './CommentFoldingRangeProvider' import { foldOtherPlatformComment } from './foldOtherPlatformComment' -function main() { - const editor = window.activeTextEditor - if (!editor) - return - - const highlightRange = getVscodeRange(editor) - setPlatformColor(highlightRange, editor) -} - -function onActiveEditorChanged(editor: TextEditor | undefined) { - if (editor) - main() +function onActiveEditorChanged() { + const range = new Ranges() + range.setColor() } function setupEventListeners() { window.onDidChangeActiveTextEditor(onActiveEditorChanged) - workspace.onDidChangeTextDocument(debounce(main, 500)) + workspace.onDidChangeTextDocument(debounce(() => { + const range = new Ranges() + range.setColor() + }, 500)) } export function activate(context: ExtensionContext) { - main() + const range = new Ranges() + range.setColor() setupEventListeners() context.subscriptions.push( @@ -35,10 +29,11 @@ export function activate(context: ExtensionContext) { new CommentFoldingRangeProvider(), ), commands.registerCommand('uni.comment.reload', () => { - main() + range.setColor() }), commands.registerCommand('uni.comment.fold-other-platform', () => { - foldOtherPlatformComment(context) + const range = new Ranges() + foldOtherPlatformComment(context, range.platformList) }), ) }