-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from zardoy/develop
- Loading branch information
Showing
23 changed files
with
212 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as vscode from 'vscode' | ||
import { defaultLanguageSupersets } from '@zardoy/vscode-utils/build/langs' | ||
import { getExtensionSetting } from 'vscode-framework' | ||
import { sendCommand } from './sendCommand' | ||
|
||
const jsxAttributesAutoTrigger = () => { | ||
vscode.workspace.onDidChangeTextDocument(async ({ contentChanges, document, reason }) => { | ||
const editor = vscode.window.activeTextEditor | ||
if (document !== editor?.document || contentChanges.length === 0) return | ||
if (contentChanges[0]!.text !== ' ') return | ||
if (![...defaultLanguageSupersets.react, 'javascript'].includes(document.languageId)) return | ||
if (!getExtensionSetting('completionsAutoTrigger.jsx')) return | ||
const path = await sendCommand('getNodePath', { document, position: editor.selection.active }) | ||
if (!path) return | ||
if (['JsxSelfClosingElement', 'JsxOpeningElement'].includes(path.at(-1)?.kindName ?? '')) { | ||
await vscode.commands.executeCommand('editor.action.triggerSuggest') | ||
} | ||
}) | ||
} | ||
|
||
export default () => { | ||
jsxAttributesAutoTrigger() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as vscode from 'vscode' | ||
import { watchExtensionSetting } from '@zardoy/vscode-utils/build/settings' | ||
import { getExtensionSetting, registerActiveDevelopmentCommand } from 'vscode-framework' | ||
|
||
// todo respect enabled setting, deactivate | ||
export default () => { | ||
const provider = new (class implements vscode.InlayHintsProvider { | ||
eventEmitter = new vscode.EventEmitter<void>() | ||
onDidChangeInlayHints = this.eventEmitter.event | ||
provideInlayHints(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): vscode.ProviderResult<vscode.InlayHint[]> { | ||
const diagnostics = vscode.languages.getDiagnostics(document.uri) | ||
const jsxMissingAttributesErrors = diagnostics.filter(({ code, source }) => (code === 2740 || code === 2739) && source === 'ts') | ||
return jsxMissingAttributesErrors | ||
.flatMap(({ range, message }) => { | ||
const regex = /: (?<prop>[\w, ]+)(?:, and (?<more>\d+) more)?\.?$/ | ||
const match = regex.exec(message) | ||
if (!match) return null as never | ||
const props = match.groups!.prop!.split(', ') | ||
const { more } = match.groups! | ||
let text = ` ${props.map(prop => `${prop}!`).join(', ')}` | ||
if (more) text += `, and ${more} more` | ||
return { | ||
kind: vscode.InlayHintKind.Type, | ||
label: text, | ||
tooltip: `Inlay hint: Missing attributes`, | ||
position: range.end, | ||
paddingLeft: true, | ||
} satisfies vscode.InlayHint | ||
// return [...props, ...(more ? [more] : [])].map((prop) => ({ | ||
// kind: vscode.InlayHintKind.Type, | ||
// label: prop, | ||
// tooltip: 'Missing attribute', | ||
// position: | ||
// })) | ||
}) | ||
.filter(Boolean) | ||
} | ||
})() | ||
let disposables = [] as vscode.Disposable[] | ||
|
||
const manageEnablement = () => { | ||
if (getExtensionSetting('inlayHints.missingJsxAttributes.enabled')) { | ||
vscode.languages.registerInlayHintsProvider('typescriptreact,javascript,javascriptreact'.split(','), provider) | ||
vscode.languages.onDidChangeDiagnostics(e => { | ||
for (const uri of e.uris) { | ||
if (uri === vscode.window.activeTextEditor?.document.uri) provider.eventEmitter.fire() | ||
} | ||
}) | ||
} else { | ||
for (const d of disposables) d.dispose() | ||
disposables = [] | ||
} | ||
} | ||
|
||
manageEnablement() | ||
watchExtensionSetting('inlayHints.missingJsxAttributes.enabled', manageEnablement) | ||
} |
44 changes: 44 additions & 0 deletions
44
typescript/src/codeActions/extended/declareMissingAttributes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ExtendedCodeAction } from '../getCodeActions' | ||
|
||
const errorCodes = [ | ||
// ts.Diagnostics.Property_0_does_not_exist_on_type_1.code, | ||
// ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code, | ||
// ts.Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2.code, | ||
// ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2.code, | ||
// ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more.code, | ||
// // ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code, | ||
// // ts.Diagnostics.Cannot_find_name_0.code, | ||
2339, 2551, 2741, 2739, 2740 /* 2345, 2304, */, | ||
] | ||
|
||
export default { | ||
codes: errorCodes, | ||
kind: 'quickfix', | ||
title: 'Declare missing attributes', | ||
tryToApply({ sourceFile, node, c, languageService, position, formatOptions, range }) { | ||
// todo maybe cache from prev request? | ||
if (!node) return | ||
const codeFixes = languageService.getCodeFixesAtPosition( | ||
sourceFile.fileName, | ||
node.getStart(), | ||
range?.end ?? node.getStart(), | ||
errorCodes, | ||
formatOptions ?? {}, | ||
{}, | ||
) | ||
const fix = codeFixes.find(codeFix => codeFix.fixName === 'fixMissingAttributes') | ||
if (fix && fix.changes[0]?.textChanges.length === 1) { | ||
const changes = fix.changes[0]!.textChanges | ||
let i = 1 | ||
return { | ||
snippetEdits: [ | ||
{ | ||
newText: changes[0]!.newText.replaceAll('$', '\\$').replaceAll('={undefined}', () => `={$${i++}}`), | ||
span: fix.changes[0]!.textChanges[0]!.span, | ||
}, | ||
], | ||
} | ||
} | ||
return | ||
}, | ||
} as ExtendedCodeAction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.