vuejs / language-tools

⚡ High-performance Vue language tooling based-on Volar.js
https://marketplace.visualstudio.com/items?itemName=Vue.volar
MIT License
5.79k stars 390 forks source link

refactor(language-service): temporarily remove references codeLens #4364

Closed johnsoncodehk closed 4 months ago

johnsoncodehk commented 4 months ago

CSS classes, template slots references codeLens no longer works since 2.0, in order to support it again we need to add a find references request for the TS plugin's name piped server, I currently have trouble finding the time to implement it, so we have temporarily removed references codeLens.

When re-implementing in the future, the following language service plugin will be needed.

import type { LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service';
import { VueVirtualCode } from '@vue/language-core';
import type * as vscode from 'vscode-languageserver-protocol';

export function create(): LanguageServicePlugin {
    return {
        name: 'vue-codelens-references',
        create(context): LanguageServicePluginInstance {
            return {

                provideReferencesCodeLensRanges(document) {

                    const decoded = context.decodeEmbeddedDocumentUri(document.uri);
                    const sourceScript = decoded && context.language.scripts.get(decoded[0]);
                    const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);

                    if (sourceScript?.generated?.root instanceof VueVirtualCode && virtualCode?.id.startsWith('style_')) {

                        const result: vscode.Range[] = [];
                        const styleIndex = parseInt(virtualCode.id.split('_')[1]);
                        const style = sourceScript.generated.root.sfc.styles[styleIndex];

                        for (const className of style.classNames) {
                            const start = document.positionAt(className.offset);
                            const end = document.positionAt(className.offset + className.text.length);
                            result.push({
                                start,
                                end,
                            });
                        }

                        return result;
                    }
                },
            };
        },
    };
}