Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-service): typescript-semantic renaming first in style blocks #4685

Merged
merged 24 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
09a81fe
fix: typescript-semantic renaming first
KazariEX Aug 13, 2024
934cd9c
fix: determine scoped
KazariEX Aug 13, 2024
111043c
feat: support module
KazariEX Aug 13, 2024
4598fb8
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Oct 23, 2024
a90cbe8
chore: deps
KazariEX Oct 23, 2024
d561158
fix: `_sfc`
KazariEX Oct 23, 2024
a92e8d2
chore: deps
KazariEX Oct 23, 2024
65a38f7
Merge remote-tracking branch 'upstream/master' into fix/typescript-se…
KazariEX Nov 2, 2024
3b334b5
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Dec 21, 2024
a283112
chore: update lockfile
KazariEX Dec 21, 2024
ea394f1
refactor: determine based on the virtual code location and features
KazariEX Dec 21, 2024
580a096
revert: re-generate property access when referencing the scoped classes
KazariEX Dec 21, 2024
51b2219
test: revert
KazariEX Dec 21, 2024
0dd7c15
chore: lint
KazariEX Dec 21, 2024
7a14882
fix: end offset calculation
KazariEX Dec 21, 2024
355bb2d
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Dec 28, 2024
6e745c4
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Jan 22, 2025
8ff946f
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Feb 15, 2025
4a3fb53
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Feb 15, 2025
3f4601f
test: restore change
KazariEX Feb 15, 2025
3a662ee
chore: lint
KazariEX Feb 15, 2025
77c95ff
Merge branch 'master' of https://github.com/vuejs/language-tools into…
KazariEX Feb 19, 2025
faf9332
fix
KazariEX Feb 19, 2025
ce80ec2
docs: add comment
KazariEX Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions packages/language-service/lib/plugins/css.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import type { LanguageServicePlugin } from '@volar/language-service';
import { create as baseCreate } from 'volar-service-css';
import type { LanguageServicePlugin, VirtualCode } from '@volar/language-service';
import { VueVirtualCode } from '@vue/language-core';
import { create as baseCreate, type Provide } from 'volar-service-css';
import * as css from 'vscode-css-languageservice';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-uri';

export function create(): LanguageServicePlugin {
const base = baseCreate({ scssDocumentSelector: ['scss', 'postcss'] });
return {
...base,
create(context) {
const baseInstance = base.create(context);
const {
'css/languageService': getCssLs,
'css/stylesheet': getStylesheet,
} = baseInstance.provide as Provide;

return {
...baseInstance,
async provideDiagnostics(document, token) {
Expand All @@ -18,7 +27,73 @@ export function create(): LanguageServicePlugin {
}
return diagnostics;
},
/**
* If the editing position is within the virtual code and navigation is enabled,
* skip the CSS renaming feature.
*/
provideRenameRange(document, position) {
do {
const uri = URI.parse(document.uri);
const decoded = context.decodeEmbeddedDocumentUri(uri);
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
if (!sourceScript?.generated || !virtualCode?.id.startsWith('style_')) {
break;
}

const root = sourceScript.generated.root;
if (!(root instanceof VueVirtualCode)) {
break;
}

const block = root.sfc.styles.find(style => style.name === decoded![1]);
if (!block) {
break;
}

let script: VirtualCode | undefined;
for (const [key, value] of sourceScript.generated.embeddedCodes) {
if (key.startsWith('script_')) {
script = value;
break;
}
}
if (!script) {
break;
}

const offset = document.offsetAt(position) + block.startTagEnd;
for (const { sourceOffsets, lengths, data } of script.mappings) {
if (
!sourceOffsets.length
|| !data.navigation
|| typeof data.navigation === 'object' && !data.navigation.shouldRename
) {
continue;
}

const start = sourceOffsets[0];
const end = sourceOffsets.at(-1)! + lengths.at(-1)!;

if (offset >= start && offset <= end) {
return;
}
}
} while (0);

return worker(document, (stylesheet, cssLs) => {
return cssLs.prepareRename(document, position, stylesheet);
});
}
};

function worker<T>(document: TextDocument, callback: (stylesheet: css.Stylesheet, cssLs: css.LanguageService) => T) {
const cssLs = getCssLs(document);
if (!cssLs) {
return;
}
return callback(getStylesheet(document, cssLs), cssLs);
}
},
};
}
1 change: 1 addition & 0 deletions packages/language-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"volar-service-pug-beautify": "0.0.62",
"volar-service-typescript": "0.0.62",
"volar-service-typescript-twoslash-queries": "0.0.62",
"vscode-css-languageservice": "^6.3.1",
"vscode-html-languageservice": "^5.2.0",
"vscode-languageserver-textdocument": "^1.0.11",
"vscode-uri": "^3.0.8"
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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