Skip to content

Commit

Permalink
refactor: get comment range data
Browse files Browse the repository at this point in the history
  • Loading branch information
FliPPeDround committed Oct 30, 2023
1 parent 76553d5 commit 2dd9556
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "@antfu"
"extends": "@antfu",
"rules": {
"no-new":"off"
}
}
11 changes: 6 additions & 5 deletions src/CommentFoldingRangeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import { Ranges } from './getVscodeRange'

export class CommentFoldingRangeProvider implements FoldingRangeProvider {
provideFoldingRanges(): ProviderResult<FoldingRange[]> {
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
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/foldOtherPlatformComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
26 changes: 14 additions & 12 deletions src/getVscodeRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export class Ranges {
editor!: TextEditor
document!: TextDocument
code!: string

static platformInfo: ReturnType<typeof getPlatformInfo>
static platformList: string[]
constructor() {
this.getContext()
this.setPlatformData()
this.hasPlatformList()
this.setColor()
}

getContext() {
Expand All @@ -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)
}
Expand Down
19 changes: 4 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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()
Expand Down

0 comments on commit 2dd9556

Please sign in to comment.