Skip to content

Commit 916678f

Browse files
committed
refactor: migrate updateVersion to internal replaceText command
1 parent 4158268 commit 916678f

9 files changed

Lines changed: 36 additions & 36 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
| `npmx.diagnostics.replacement` | Show suggestions for package replacements | `boolean` | `true` |
4747
| `npmx.diagnostics.vulnerability` | Show warnings for packages with known vulnerabilities | `boolean` | `true` |
4848
| `npmx.diagnostics.distTag` | Show warnings when a dependency uses a dist tag | `boolean` | `true` |
49-
| `npmx.versionLens.enabled` | Show version lens (CodeLens) for package dependencies | `boolean` | `true` |
49+
| `npmx.versionLens.enabled` | Show version lens (CodeLens) for package dependencies | `boolean` | `false` |
5050

5151
<!-- configs -->
5252

eslint.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export default defineConfig(
77
},
88
{
99
files: ['src/commands/**'],
10+
ignores: ['**/index.ts'],
1011
rules: {
1112
'no-restricted-imports': ['error', {
1213
paths: [{
1314
name: 'reactive-vscode',
15+
allowImportNames: ['useCommand', 'useCommands', 'useTextEditorCommand', 'useTextEditorCommands'],
16+
allowTypeImports: true,
1417
message: 'Do not use reactive-vscode composables in command handlers. Use vscode API directly.',
1518
}],
1619
}],

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@
111111
"command": "npmx.openFileInNpmx",
112112
"title": "Open file on npmx.dev",
113113
"category": "npmx"
114-
},
115-
{
116-
"command": "npmx.updateVersion",
117-
"title": "Update package version",
118-
"category": "npmx"
119114
}
120115
],
121116
"menus": {

src/commands/internal/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { internalCommands } from '#state'
2+
import { useTextEditorCommands } from 'reactive-vscode'
3+
import { replaceText } from './replace-text'
4+
5+
export function useInternalCommands() {
6+
useTextEditorCommands({
7+
[internalCommands.replaceText]: replaceText,
8+
})
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { TextEditorCommandCallback } from 'reactive-vscode'
2+
import type { Range, TextEditor, TextEditorEdit } from 'vscode'
3+
4+
export const replaceText: TextEditorCommandCallback = (_: TextEditor, edit: TextEditorEdit, range?: Range, text?: string) => {
5+
if (!range || !text)
6+
return
7+
8+
edit.replace(range, text)
9+
}

src/commands/update-version.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { VERSION_TRIGGER_CHARACTERS } from '#constants'
22
import { defineExtension, useCommands, watchEffect } from 'reactive-vscode'
33
import { Disposable, languages } from 'vscode'
4+
import { useInternalCommands } from './commands/internal'
45
import { openFileInNpmx } from './commands/open-file-in-npmx'
56
import { openInBrowser } from './commands/open-in-browser'
6-
import { updateVersion } from './commands/update-version'
77
import { extractorEntries } from './extractors'
88
import { commands, displayName, version } from './generated-meta'
99
import { useCodeActions } from './providers/code-actions'
@@ -42,13 +42,13 @@ export const { activate, deactivate } = defineExtension(() => {
4242
onCleanup(() => Disposable.from(...disposables).dispose())
4343
})
4444

45+
useInternalCommands()
4546
useDiagnostics()
4647
useCodeActions()
4748
useCodeLens()
4849

4950
useCommands({
5051
[commands.openInBrowser]: openInBrowser,
5152
[commands.openFileInNpmx]: openFileInNpmx,
52-
[commands.updateVersion]: updateVersion,
5353
})
5454
})

src/providers/code-lens/version.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import type { DependencyInfo, Extractor } from '#types/extractor'
2-
import type { CodeLensProvider, Range, TextDocument } from 'vscode'
2+
import type { CodeLensProvider, TextDocument } from 'vscode'
3+
import { internalCommands } from '#state'
34
import { getPackageInfo } from '#utils/api/package'
45
import { getUpdateType } from '#utils/semver'
56
import { formatVersion, isSupportedProtocol, parseVersion } from '#utils/version'
67
import { debounce } from 'perfect-debounce'
78
import { CodeLens, EventEmitter } from 'vscode'
8-
import { commands } from '../../generated-meta'
99

10-
interface LensData {
11-
dep: DependencyInfo
12-
versionRange: Range
13-
uri: TextDocument['uri']
14-
}
15-
16-
const dataMap = new WeakMap<CodeLens, LensData>()
10+
const dataMap = new WeakMap<CodeLens, DependencyInfo>()
1711

1812
export class VersionCodeLensProvider<T extends Extractor> implements CodeLensProvider {
1913
extractor: T
@@ -42,19 +36,18 @@ export class VersionCodeLensProvider<T extends Extractor> implements CodeLensPro
4236

4337
const versionRange = this.extractor.getNodeRange(document, dep.versionNode)
4438
const lens = new CodeLens(versionRange)
45-
dataMap.set(lens, { dep, versionRange, uri: document.uri })
39+
dataMap.set(lens, dep)
4640
lenses.push(lens)
4741
}
4842

4943
return lenses
5044
}
5145

5246
resolveCodeLens(lens: CodeLens) {
53-
const data = dataMap.get(lens)
54-
if (!data)
47+
const dep = dataMap.get(lens)
48+
if (!dep)
5549
return lens
5650

57-
const { dep, versionRange, uri } = data
5851
const parsed = parseVersion(dep.version)
5952
if (!parsed) {
6053
lens.command = { title: '$(question) unknown', command: '' }
@@ -82,8 +75,8 @@ export class VersionCodeLensProvider<T extends Extractor> implements CodeLensPro
8275
const newVersion = formatVersion({ ...parsed, semver: latest })
8376
lens.command = {
8477
title: `$(arrow-up) ${newVersion} (${updateType})`,
85-
command: commands.updateVersion,
86-
arguments: [uri, versionRange, newVersion],
78+
command: internalCommands.replaceText,
79+
arguments: [lens.range, newVersion],
8780
}
8881
}
8982

src/state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ import { displayName, scopedConfigs } from './generated-meta'
55
export const config = defineConfig<NestedScopedConfigs>(scopedConfigs.scope)
66

77
export const logger = defineLogger(displayName)
8+
9+
export const internalCommands = {
10+
replaceText: 'npmx.internal.replaceText',
11+
}

0 commit comments

Comments
 (0)