From 33ae0565e9d02dd682921a5d2b3b8f4e13ade6c8 Mon Sep 17 00:00:00 2001 From: Ilya Golovin <74474615+ILScc@users.noreply.github.com> Date: Wed, 29 Jun 2022 15:26:56 +0300 Subject: [PATCH] fix(type-decorations): disable in style blocks (#7) --- package.json | 5 +++++ pnpm-lock.yaml | 24 ++++++++++++++++++++++++ src/features/typeDecorations.ts | 32 ++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ed6e475..a9a1db8 100644 --- a/package.json +++ b/package.json @@ -227,6 +227,10 @@ "type": "boolean", "default": true }, + "typeDecorations.enableInStyles": { + "type": "boolean", + "default": false + }, "autoRemoveSemicolon.enable": { "type": "boolean", "default": true @@ -309,6 +313,7 @@ "hosted-git-info": "^4.1.0", "ini": "^2.0.0", "lower-case-first": "^2.0.2", + "markdown-to-txt": "^2.0.1", "minimatch": "^5.0.1", "open": "^8.4.0", "path-browserify": "^1.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4930ef2..b08ad94 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,6 +21,7 @@ specifiers: hosted-git-info: ^4.1.0 ini: ^2.0.0 lower-case-first: ^2.0.2 + markdown-to-txt: ^2.0.1 minimatch: ^5.0.1 open: ^8.4.0 path-browserify: ^1.0.1 @@ -42,6 +43,7 @@ dependencies: hosted-git-info: 4.1.0 ini: 2.0.0 lower-case-first: 2.0.2 + markdown-to-txt: 2.0.1 minimatch: 5.0.1 open: 8.4.0 path-browserify: 1.0.1 @@ -2907,10 +2909,18 @@ packages: resolution: {integrity: sha1-VAzjg3dFl1gHRx4WtKK6IeclbKU=} dev: false + /lodash.escape/4.0.1: + resolution: {integrity: sha512-nXEOnb/jK9g0DYMr1/Xvq6l5xMD7GDG55+GSYIYmS0G4tBk/hURD4JR9WCavs04t33WmJx9kCyp9vJ+mr4BOUw==} + dev: false + /lodash.merge/4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} dev: true + /lodash.unescape/4.0.1: + resolution: {integrity: sha512-DhhGRshNS1aX6s5YdBE3njCCouPgnG29ebyHvImlZzXZf2SHgt+J08DHgytTPnpywNbO1Y8mNUFyQuIDBq2JZg==} + dev: false + /lodash/4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} @@ -2949,6 +2959,20 @@ packages: resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==} dev: false + /markdown-to-txt/2.0.1: + resolution: {integrity: sha512-Hsj7KTN8k1gutlLum3vosHwVZGnv8/cbYKWVkUyo/D1rzOYddbDesILebRfOsaVfjIBJank/AVOySBlHAYqfZw==} + dependencies: + lodash.escape: 4.0.1 + lodash.unescape: 4.0.1 + marked: 4.0.17 + dev: false + + /marked/4.0.17: + resolution: {integrity: sha512-Wfk0ATOK5iPxM4ptrORkFemqroz0ZDxp5MWfYA7H/F+wO17NRWV5Ypxi6p3g2Xmw2bKeiYOl6oVnLHKxBA0VhA==} + engines: {node: '>= 12'} + hasBin: true + dev: false + /media-typer/0.3.0: resolution: {integrity: sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=} engines: {node: '>= 0.6'} diff --git a/src/features/typeDecorations.ts b/src/features/typeDecorations.ts index ef1a37b..2ec0df7 100644 --- a/src/features/typeDecorations.ts +++ b/src/features/typeDecorations.ts @@ -1,5 +1,7 @@ import * as vscode from 'vscode' import { getExtensionSetting } from 'vscode-framework' +import { getNormalizedVueOutline } from '@zardoy/vscode-utils/build/vue' +import { markdownToTxt } from 'markdown-to-txt' export default () => { if (!getExtensionSetting('typeDecorations.enable')) return @@ -12,6 +14,26 @@ export default () => { }, rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, }) + + const checkIfInStyles = async (document: vscode.TextDocument, position: vscode.Position) => { + const { languageId, uri } = document + const stylesLangs = new Set(['scss', 'css', 'less', 'sass']) + if (stylesLangs.has(languageId)) return true + if (languageId === 'vue') { + const outline = await getNormalizedVueOutline(uri) + if (!outline) { + console.warn('No default vue outline. Install Volar or Vetur') + return true + } + + const style = outline.find(item => item.name === 'style') + if (style?.range.contains(position)) return true + return false + } + + return false + } + const checkDecorations = async ({ textEditor: editor }: { textEditor?: vscode.TextEditor } = {}): Promise => { const textEditor = vscode.window.activeTextEditor if ( @@ -21,16 +43,18 @@ export default () => { !vscode.languages.match(enableLanguages, textEditor.document) ) return - const { selections } = textEditor + const { selections, document } = textEditor const pos = selections[0]!.end - const { document } = textEditor const text = document.lineAt(pos).text.slice(0, pos.character) const match = /(:| =) $/.exec(text) textEditor.setDecorations(decoration, []) if (!match) return const offset = match[0]!.length + const isInStyles = await checkIfInStyles(document, pos) + if (isInStyles && !getExtensionSetting('typeDecorations.enableInStyles')) return const hoverData: vscode.Hover[] = await vscode.commands.executeCommand('vscode.executeHoverProvider', document.uri, pos.translate(0, -offset)) let typeString: string | undefined + for (const hover of hoverData) { const hoverString = hover.contents .map(content => { @@ -38,9 +62,9 @@ export default () => { return content }) .join('') - const typeMatch = /: (.+)/.exec(hoverString) + const typeMatch = isInStyles ? /Syntax: (.*)/.exec(hoverString) : /: (.+)/.exec(hoverString) if (!typeMatch) continue - typeString = typeMatch[1]! + typeString = markdownToTxt(typeMatch[1]!) break }