Skip to content

Commit

Permalink
feat(#182): enable references,codelens,rename,diagnostics only for mo…
Browse files Browse the repository at this point in the history
…dules
  • Loading branch information
Viijay-Kr committed Dec 25, 2024
1 parent 50c40d6 commit ff52071
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
workspace,
languages,
extensions,
DocumentSelector,
} from "vscode";
import Settings, { EXT_NAME, getSettings } from "./settings";
import Store from "./store/Store";
Expand All @@ -31,22 +32,22 @@ const documentSelector = [
{ scheme: "file", language: "javascript" },
];

const cssDocumentSelector = [
const cssDocumentSelector: DocumentSelector = [
{
scheme: "file",
language: "css",
pattern: "**/*.module.css",
},
];

const cssModulesDocumentSelector = [
const cssModulesDocumentSelector: DocumentSelector = [
...cssDocumentSelector,
{
scheme: "file",
language: "scss",
pattern: "**/*.module.scss",
},
{
scheme: "file",
language: "less",
pattern: "**/*.module.less",
},
];

Expand Down Expand Up @@ -157,12 +158,12 @@ export async function activate(context: ExtensionContext): Promise<void> {
);

const _cssColorProviders = languages.registerColorProvider(
cssDocumentSelector,
{ scheme: "file", language: "css" },
new CssDocumentColorProvider(),
);

const _cssDefinitionProvider = languages.registerDefinitionProvider(
cssDocumentSelector,
{ scheme: "file", language: "css" },
new CssDefinitionProvider(),
);

Expand Down
4 changes: 4 additions & 0 deletions src/providers/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ export class ImportsRelatedDiagnostics extends Diagnostics {

export class CSSDocumentDiagnostics extends Diagnostics {
protected cssDiagnosticsProvider: CSSDiagnosticsProvider;
protected uri: Uri;
constructor(options: DiagnosticsContext) {
super(options);
this.uri = options.document.uri;
this.cssDiagnosticsProvider = new CSSDiagnosticsProvider({
providerKind: ProviderKind.Diagnostic,
position: new Position(0, 0),
Expand All @@ -247,6 +249,8 @@ export class CSSDocumentDiagnostics extends Diagnostics {
}

async runDiagnostics() {
if (!this.uri.fsPath.includes("module")) return;

this.diagnostics = await this.cssDiagnosticsProvider.provideDiagnostics();
}
}
16 changes: 14 additions & 2 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import {
import { parseCss } from "../../parser/v2/css";
import {
CSSCodeLensProvider,
CSSProvider,
CSSReferenceProvider,
CSSRenameProvider,
} from "../../providers/css/CSSProvider";
import { ProviderKind } from "../../providers/types";
const examplesLocation = "../../../examples/";

const testAppLocation = path.join(__dirname, examplesLocation, "react-app");

function setWorskpaceFolder(app: string) {
workspace.getWorkspaceFolder = () => {
return {
Expand Down Expand Up @@ -365,16 +366,18 @@ suite("Extension Test Suite", async () => {
await window.showTextDocument(document);
const diagnostics = await Storage.bootstrap();
assert.equal(diagnostics?.length, 1);
Storage.flushStorage();
});

test("should provide diagnostics for in correct css module import", async () => {
const document = await workspace.openTextDocument(DiagnosticComponent);
await window.showTextDocument(document);
const diagnostics = await Storage.bootstrap();
assert.equal(diagnostics?.length, 2);
Storage.flushStorage();
});

test("should not provide dignostics for dynamic slectors", async () => {
test("should not provide dignostics for dynamic selectors", async () => {
const DynamicClasses = Uri.file(
path.join(
__dirname,
Expand All @@ -386,6 +389,15 @@ suite("Extension Test Suite", async () => {
await window.showTextDocument(document);
const diagnostics = await Storage.bootstrap();
assert.equal(diagnostics?.length, 0);
Storage.flushStorage();
});

test("should not provide diagnostics for normal css files", async () => {
const AppCss = Uri.file(path.join(testAppLocation, "src/App.css"));
const document = await workspace.openTextDocument(AppCss);
await window.showTextDocument(document);
const diagnostics = await Storage.bootstrap();
assert.equal(diagnostics?.length, 0);
});
});
});
Expand Down

0 comments on commit ff52071

Please sign in to comment.