Skip to content

Commit

Permalink
feat: stricter type decorations (#11)
Browse files Browse the repository at this point in the history
- fix: only show decoration if selection is empty
- now decorations showing only in case if there is no existing value
- new settings to filter out some decoration values
- disable after `const` or `let`
  • Loading branch information
zardoy authored Jul 1, 2022
1 parent d1c23d3 commit 5ad99a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@
"type": "boolean",
"default": true
},
"typeDecorations.ignoreValues": {
"type": "array",
"uniqueItems": true,
"markdownDescription": "Ignore specific resolved values from type decorations. In future it can be regex if starts & ends with `/`. Examples can be: `any`, `unknown`",
"items": {
"type": "string"
}
},
"typeDecorations.enableInStyles": {
"type": "boolean",
"default": false
Expand Down
22 changes: 16 additions & 6 deletions src/features/typeDecorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { markdownToTxt } from 'markdown-to-txt'
export default () => {
if (!getExtensionSetting('typeDecorations.enable')) return
const enableLanguages = getExtensionSetting('typeDecorations.languages')
const ignoreValues = getExtensionSetting('typeDecorations.ignoreValues')
const decoration = vscode.window.createTextEditorDecorationType({
after: {
// https://code.visualstudio.com/api/references/theme-color#editor-colors
Expand Down Expand Up @@ -43,12 +44,21 @@ export default () => {
!vscode.languages.match(enableLanguages, textEditor.document)
)
return
const { selections, document } = textEditor
const pos = selections[0]!.end
const text = document.lineAt(pos).text.slice(0, pos.character)
const match = /(:| =) $/.exec(text)
textEditor.setDecorations(decoration, [])
if (!match) return
const {
selections: [selection],
} = textEditor
if (!selection || !selection.isEmpty) return
const pos = selection.end
const { document } = textEditor
const lineText = document.lineAt(pos).text
const textBefore = lineText.slice(0, pos.character)
const textAfter = lineText.slice(pos.character)
const match = /(:| =) $/.exec(textBefore)
const isInBannedPosition = /(const|let) (\w|\d)+ = $/i.test(textBefore)
// if in destructure or object literal
const endingMatch = /^\s*(}|]|;|,|$)/
if (!match || isInBannedPosition || !endingMatch.test(textAfter)) return
const offset = match[0]!.length
const isInStyles = await checkIfInStyles(document, pos)
if (isInStyles && !getExtensionSetting('typeDecorations.enableInStyles')) return
Expand All @@ -68,7 +78,7 @@ export default () => {
break
}

if (!typeString || typeString === '{') return
if (!typeString || typeString === '{' || ignoreValues.includes(typeString)) return
textEditor.setDecorations(decoration, [
{
range: new vscode.Range(pos.translate(0, -1), pos),
Expand Down

0 comments on commit 5ad99a3

Please sign in to comment.