Skip to content

Commit 0d34cb0

Browse files
authored
Merge pull request #92 from zardoy/develop
2 parents 936fc4a + 55c468d commit 0d34cb0

17 files changed

+351
-36
lines changed

README.MD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,34 @@ Some settings examples:
228228
> Note: changeSorting might not preserve sorting of other existing suggestions which not defined by rules, there is WIP
229229
> Also I'm thinking of making it learn and syncing of most-used imports automatically
230230
231+
## Rename Features
232+
233+
There is builtin mechanism to rename variable occurrences in strings & comments, it is disabled in VS Code without a way to enable it.
234+
235+
However this extension also has builtin keybinding `Ctrl+Shift+Enter` that can be pressed when input box is visible to enable aforementioned behavior for renaming with preview.
236+
237+
But note renaming in strings & comments will happen only for files in which variable is actually referenced.
238+
239+
You can add this to `keybindings.json` to disable previewing before renaming:
240+
241+
```js
242+
{
243+
"key": "ctrl+shift+enter",
244+
"command": "tsEssentialPlugins.acceptRenameWithParams",
245+
"args": {
246+
"strings": true,
247+
"comments": true,
248+
// "preview": true // true by default
249+
// "alias": true // you can also specify here wether to introduce alias on rename if applicable (overriding global setting)
250+
},
251+
"when": "renameInputVisible"
252+
}
253+
```
254+
255+
Another options that is accepted is
256+
257+
> Note: VS Code has builtin setting to disable introducing aliases (e.g. for imports & object properties)
258+
231259
## Special Commands List
232260

233261
### Go to / Select Nodes by Kind

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@
5353
"category": "TS Essentials"
5454
}
5555
],
56+
"keybindings": [
57+
{
58+
"key": "ctrl+shift+enter",
59+
"mac": "cmd+shift+enter",
60+
"command": "tsEssentialPlugins.acceptRenameWithParams",
61+
"args": {
62+
"strings": true,
63+
"comments": true,
64+
"preview": true
65+
},
66+
"when": "renameInputVisible && editorLangId =~ /javascript|javascriptreact|typescript|typescriptreact|vue/"
67+
}
68+
],
5669
"typescriptServerPlugins": [
5770
{
5871
"name": "typescript-essential-plugins",
@@ -105,7 +118,7 @@
105118
"type-fest": "^2.13.1",
106119
"typed-jsonfile": "^0.2.1",
107120
"typescript": "^4.9.3",
108-
"vitest": "^0.25.3",
121+
"vitest": "^0.26.0",
109122
"vscode-manifest": "^0.0.4"
110123
},
111124
"pnpm": {

pnpm-lock.yaml

Lines changed: 90 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configurationType.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ export type Configuration = {
6363
* @default false
6464
*/
6565
enableVueSupport: boolean
66-
/**
67-
* @default true
68-
*/
69-
vueSpecificImprovements: boolean
7066
/**
7167
* Temporary setting to enable loading config from other locations (also to expose plugin)
7268
*/

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import apiCommands from './apiCommands'
1414
import onCompletionAccepted from './onCompletionAccepted'
1515
import specialCommands from './specialCommands'
1616
import vueVolarSupport from './vueVolarSupport'
17+
import moreCompletions from './moreCompletions'
1718

1819
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted }) => {
1920
let webWaitingForConfigSync = false
@@ -65,6 +66,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
6566
}
6667

6768
experimentalPostfixes()
69+
moreCompletions()
6870
void registerEmmet()
6971
webImports()
7072
apiCommands()

src/moreCompletions.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as vscode from 'vscode'
2+
import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'
3+
4+
export default () => {
5+
vscode.languages.registerCompletionItemProvider(
6+
defaultJsSupersetLangsWithVue,
7+
{
8+
provideCompletionItems(document, position, token, context) {
9+
const regex = /\/\/@?[\w-]*/
10+
let range = document.getWordRangeAtPosition(position, regex)
11+
if (!range) return
12+
const rangeText = document.getText(range)
13+
if (rangeText !== document.lineAt(position).text.trim()) {
14+
return
15+
}
16+
17+
range = range.with(range.start.translate(0, 2), range.end)
18+
const tsDirectives = ['@ts-format-ignore-line', '@ts-format-ignore-region', '@ts-format-ignore-endregion']
19+
return tsDirectives.map((directive, i) => {
20+
const completionItem = new vscode.CompletionItem(directive, vscode.CompletionItemKind.Snippet)
21+
completionItem.range = range
22+
completionItem.sortText = `z${i}`
23+
return completionItem
24+
})
25+
},
26+
},
27+
'@',
28+
)
29+
}

src/specialCommands.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ export default () => {
265265
await vscode.workspace.applyEdit(edit)
266266
})
267267

268+
registerExtensionCommand('acceptRenameWithParams' as any, async (_, { preview = false, comments = null, strings = null, alias = null } = {}) => {
269+
const editor = vscode.window.activeTextEditor
270+
if (!editor) return
271+
const {
272+
document,
273+
selection: { active: position },
274+
} = editor
275+
await sendCommand<RequestOptionsTypes['acceptRenameWithParams']>('acceptRenameWithParams', {
276+
document,
277+
position,
278+
inputOptions: {
279+
alias,
280+
comments,
281+
strings,
282+
} satisfies RequestOptionsTypes['acceptRenameWithParams'],
283+
})
284+
await vscode.commands.executeCommand(preview ? 'acceptRenameInputWithPreview' : 'acceptRenameInput')
285+
})
286+
268287
// its actually a code action, but will be removed from there soon
269288
vscode.languages.registerCodeActionsProvider(defaultJsSupersetLangsWithVue, {
270289
async provideCodeActions(document, range, context, token) {

typescript/src/codeFixes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
6565
} finally {
6666
tsFull.codefix.createCodeFixAction = oldCreateCodeFixAction
6767
}
68+
// todo remove when 5.0 is released after 3 months
6869
// #region fix builtin codefixes/refactorings
6970
prior.forEach(fix => {
7071
if (fix.fixName === 'fixConvertConstToLet') {

typescript/src/completions/arrayMethods.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GetConfig } from '../types'
22
import { findChildContainingPosition, getLineTextBeforePos } from '../utils'
3-
import { singular } from 'pluralize'
3+
import pluralize from 'pluralize'
44

55
const arrayMethodsToPatch = [
66
'forEach',
@@ -31,7 +31,7 @@ export default (entries: ts.CompletionEntry[], position: number, sourceFile: ts.
3131
if (!nodeBeforeDot) return
3232

3333
const cleanSourceText = getItemNameFromNode(nodeBeforeDot)?.replace(/^(?:all)?(.+?)(?:List)?$/, '$1')
34-
let inferredName = cleanSourceText && singular(cleanSourceText)
34+
let inferredName = cleanSourceText && pluralize.singular(cleanSourceText)
3535
const defaultItemName = c('arrayMethodsSnippets.defaultItemName')
3636
// both can be undefined
3737
if (inferredName === cleanSourceText) {

0 commit comments

Comments
 (0)