Skip to content

Commit

Permalink
Merge pull request #92 from zardoy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Feb 2, 2023
2 parents 936fc4a + 55c468d commit 0d34cb0
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 36 deletions.
28 changes: 28 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,34 @@ Some settings examples:
> Note: changeSorting might not preserve sorting of other existing suggestions which not defined by rules, there is WIP
> Also I'm thinking of making it learn and syncing of most-used imports automatically
## Rename Features

There is builtin mechanism to rename variable occurrences in strings & comments, it is disabled in VS Code without a way to enable it.

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.

But note renaming in strings & comments will happen only for files in which variable is actually referenced.

You can add this to `keybindings.json` to disable previewing before renaming:

```js
{
"key": "ctrl+shift+enter",
"command": "tsEssentialPlugins.acceptRenameWithParams",
"args": {
"strings": true,
"comments": true,
// "preview": true // true by default
// "alias": true // you can also specify here wether to introduce alias on rename if applicable (overriding global setting)
},
"when": "renameInputVisible"
}
```

Another options that is accepted is

> Note: VS Code has builtin setting to disable introducing aliases (e.g. for imports & object properties)
## Special Commands List

### Go to / Select Nodes by Kind
Expand Down
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@
"category": "TS Essentials"
}
],
"keybindings": [
{
"key": "ctrl+shift+enter",
"mac": "cmd+shift+enter",
"command": "tsEssentialPlugins.acceptRenameWithParams",
"args": {
"strings": true,
"comments": true,
"preview": true
},
"when": "renameInputVisible && editorLangId =~ /javascript|javascriptreact|typescript|typescriptreact|vue/"
}
],
"typescriptServerPlugins": [
{
"name": "typescript-essential-plugins",
Expand Down Expand Up @@ -105,7 +118,7 @@
"type-fest": "^2.13.1",
"typed-jsonfile": "^0.2.1",
"typescript": "^4.9.3",
"vitest": "^0.25.3",
"vitest": "^0.26.0",
"vscode-manifest": "^0.0.4"
},
"pnpm": {
Expand Down
101 changes: 90 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export type Configuration = {
* @default false
*/
enableVueSupport: boolean
/**
* @default true
*/
vueSpecificImprovements: boolean
/**
* Temporary setting to enable loading config from other locations (also to expose plugin)
*/
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import apiCommands from './apiCommands'
import onCompletionAccepted from './onCompletionAccepted'
import specialCommands from './specialCommands'
import vueVolarSupport from './vueVolarSupport'
import moreCompletions from './moreCompletions'

export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted }) => {
let webWaitingForConfigSync = false
Expand Down Expand Up @@ -65,6 +66,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
}

experimentalPostfixes()
moreCompletions()
void registerEmmet()
webImports()
apiCommands()
Expand Down
29 changes: 29 additions & 0 deletions src/moreCompletions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode'
import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'

export default () => {
vscode.languages.registerCompletionItemProvider(
defaultJsSupersetLangsWithVue,
{
provideCompletionItems(document, position, token, context) {
const regex = /\/\/@?[\w-]*/
let range = document.getWordRangeAtPosition(position, regex)
if (!range) return
const rangeText = document.getText(range)
if (rangeText !== document.lineAt(position).text.trim()) {
return
}

range = range.with(range.start.translate(0, 2), range.end)
const tsDirectives = ['@ts-format-ignore-line', '@ts-format-ignore-region', '@ts-format-ignore-endregion']
return tsDirectives.map((directive, i) => {
const completionItem = new vscode.CompletionItem(directive, vscode.CompletionItemKind.Snippet)
completionItem.range = range
completionItem.sortText = `z${i}`
return completionItem
})
},
},
'@',
)
}
19 changes: 19 additions & 0 deletions src/specialCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ export default () => {
await vscode.workspace.applyEdit(edit)
})

registerExtensionCommand('acceptRenameWithParams' as any, async (_, { preview = false, comments = null, strings = null, alias = null } = {}) => {
const editor = vscode.window.activeTextEditor
if (!editor) return
const {
document,
selection: { active: position },
} = editor
await sendCommand<RequestOptionsTypes['acceptRenameWithParams']>('acceptRenameWithParams', {
document,
position,
inputOptions: {
alias,
comments,
strings,
} satisfies RequestOptionsTypes['acceptRenameWithParams'],
})
await vscode.commands.executeCommand(preview ? 'acceptRenameInputWithPreview' : 'acceptRenameInput')
})

// its actually a code action, but will be removed from there soon
vscode.languages.registerCodeActionsProvider(defaultJsSupersetLangsWithVue, {
async provideCodeActions(document, range, context, token) {
Expand Down
1 change: 1 addition & 0 deletions typescript/src/codeFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
} finally {
tsFull.codefix.createCodeFixAction = oldCreateCodeFixAction
}
// todo remove when 5.0 is released after 3 months
// #region fix builtin codefixes/refactorings
prior.forEach(fix => {
if (fix.fixName === 'fixConvertConstToLet') {
Expand Down
4 changes: 2 additions & 2 deletions typescript/src/completions/arrayMethods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetConfig } from '../types'
import { findChildContainingPosition, getLineTextBeforePos } from '../utils'
import { singular } from 'pluralize'
import pluralize from 'pluralize'

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

const cleanSourceText = getItemNameFromNode(nodeBeforeDot)?.replace(/^(?:all)?(.+?)(?:List)?$/, '$1')
let inferredName = cleanSourceText && singular(cleanSourceText)
let inferredName = cleanSourceText && pluralize.singular(cleanSourceText)
const defaultItemName = c('arrayMethodsSnippets.defaultItemName')
// both can be undefined
if (inferredName === cleanSourceText) {
Expand Down
Loading

0 comments on commit 0d34cb0

Please sign in to comment.