Skip to content

Commit

Permalink
feat: get current platform && icon btn
Browse files Browse the repository at this point in the history
  • Loading branch information
FliPPeDround committed Oct 21, 2023
1 parent 604df6c commit ccd888a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
{
"command": "uni.comment.fold-other-platform",
"title": "Uni Helper: 选择平台并折叠其他平台注释"
"title": "Uni Helper: 选择平台并折叠其他平台注释",
"icon": "$(list-tree)"
}
],
"configuration": {
Expand All @@ -55,6 +56,15 @@
"description": "自定义平台高亮"
}
}
},
"menus": {
"editor/title": [
{
"command": "uni.comment.fold-other-platform",
"group": "navigation@0",
"when": "uni.hasComment"
}
]
}
},
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions src/foldOtherPlatformComment.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/getPlatformInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function getPlatformInfo(code: string): PlatformInfo[] {
end,
type,
color,
row,
})
}
else if (type === 'platform' && !color) {
Expand Down
49 changes: 43 additions & 6 deletions src/getVscodeRange.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
33 changes: 14 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
}),
)
}
Expand Down

0 comments on commit ccd888a

Please sign in to comment.