Skip to content

Commit

Permalink
feat: add fold other platforms command
Browse files Browse the repository at this point in the history
feat: add fold other platforms command
  • Loading branch information
FliPPeDround authored Oct 19, 2023
2 parents 9502f9e + 75e8379 commit 1d81e20
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
playground/**/*.*
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center"><font color="#2a9838">uni-helper/uni-highlight-vscode</font></h1>
<p align="center">
<a href="https://marketplace.visualstudio.com/items?itemName=uni-helper.uni-highlight-vscode" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/uni-helper.uni-highlight-vscode.svg?color=4d9375&amp;label=Marketplace&logo=visual-studio-code" alt="Visual Studio Marketplace Version" /></a>
<a href="https://open-vsx.org/extension/uni-helper/uni-highlight-vscode" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/uni-helper.uni-highlight-vscode.svg?color=c160ef&amp;label=OpenVSX&logo=OpenVSX" alt="Visual Studio Marketplace Version" /></a>
<a href="https://marketplace.visualstudio.com/items?itemName=uni-helper.uni-highlight-vscode" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/uni-helper.uni-highlight-vscode.svg?color=4d9375&label=Marketplace&logo=visual-studio-code" alt="Visual Studio Marketplace Version" /></a>
<a href="https://open-vsx.org/extension/uni-helper/uni-highlight-vscode" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/uni-helper.uni-highlight-vscode.svg?color=c160ef&label=OpenVSX&logo=OpenVSX" alt="Visual Studio Marketplace Version" /></a>
</p>

## VS Code条件编译语法高亮支持
Expand Down Expand Up @@ -34,7 +34,7 @@

### 自定义高亮

```jsonc
```json5
/**
* 文件路径:.vscode/settings.json
* 配置项:uni-highlight.platform
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
"commands": [
{
"command": "uni.comment.reload",
"title": "UniHelper: Reload Comment"
"title": "Uni Helper: 重新加载条件编译高亮"
},
{
"command": "uni.comment.fold-other-platform",
"title": "Uni Helper: 选择平台并折叠其他平台注释"
}
],
"configuration": {
Expand Down
14 changes: 9 additions & 5 deletions playground/test.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// #ifdef H5
const test = 'text'
// #endif
// #ifdef MP
const mp = 'text'
// #endif
</script>

<template>
Expand All @@ -13,9 +16,10 @@ const test = 'text'
</template>

<style>
/* #ifdef H5 */
.test {
color: red;
}
/* #endif */
/* #ifdef H5 */
.test {
color: red;
}
/* #endif */
</style>
53 changes: 53 additions & 0 deletions src/foldOtherPlatformComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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) {
const platform = await window.showQuickPick([
...PLATFORM_LIST,
])

if (!platform)
return

const editor = window.activeTextEditor
const document = editor?.document

if (document) {
const c = new CommentFoldingRangeProvider()
const ranges = c.provideFoldingRanges(document, undefined as any, undefined as any)
if (Array.isArray(ranges)) {
const comments = ranges.map((v) => {
const text = document.getText(lineNumberToRange(v.start))
return {
start: v.start,
flag: !text?.includes(platform),
}
})
const fold = comments.filter(v => v.flag).map(v => v.start)
const unfold = comments.filter(v => !v.flag).map(v => v.start)
await commands.executeCommand('editor.unfold', {
levels: 1,
direction: 'up',
selectionLines: unfold,
})
await commands.executeCommand('editor.fold', {
levels: 1,
direction: 'up',
selectionLines: fold,
})
}
}

function lineNumberToRange(line: number): Range {
// Convert the line number to a position
const startPosition = new Position(line, 0)
const endPosition = new Position(line, Number.MAX_SAFE_INTEGER)

// Create a range using the start and end positions
const range = new Range(startPosition, endPosition)

return range
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getVscodeRange } from './getVscodeRange'
import { setPlatformColor } from './setPlatformColor'
import { debounce } from './utils'
import { CommentFoldingRangeProvider } from './CommentFoldingRangeProvider'
import { foldOtherPlatformComment } from './foldOtherPlatformComment'

function main() {
const editor = window.activeTextEditor
Expand Down Expand Up @@ -36,6 +37,9 @@ export function activate(context: ExtensionContext) {
commands.registerCommand('uni.comment.reload', () => {
main()
}),
commands.registerCommand('uni.comment.fold-other-platform', () => {
foldOtherPlatformComment(context)
}),
)
}

Expand Down
14 changes: 13 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { getPlatformInfo } from './../src/getPlatformInfo'

vi.mock('vscode', () => {
return {
workspace: {
getConfiguration: () => {
return {
get: vi.fn(),
}
},
},
}
})

describe('getPlatformInfo', () => {
it('get // #endif', () => {
const code = `
Expand Down
26 changes: 19 additions & 7 deletions test/parse/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { parsePlatform } from '../../src/parseComment/parsePlatform'
import { parseComment } from './../../src/parseComment'
import { parseComment } from '../../src/parseComment'

vi.mock('vscode', () => {
return {
workspace: {
getConfiguration: () => {
return {
get: vi.fn(),
}
},
},
}
})

describe('parseComment', () => {
it('should parse comment', () => {
const code = `
// #ifdef APP-PLUSaasd
// #endif
// #endif
/* #ifdef APP-PLUS */
`
expect(parseComment(code)).toMatchInlineSnapshot(`
Expand All @@ -30,15 +42,15 @@ describe('parseComment', () => {
"type": "prefix",
},
{
"end": 59,
"end": 55,
"row": "#ifdef",
"start": 53,
"start": 49,
"type": "prefix",
},
{
"end": 68,
"end": 64,
"row": "APP-PLUS",
"start": 60,
"start": 56,
"type": "platform",
},
]
Expand Down

0 comments on commit 1d81e20

Please sign in to comment.