Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Setting to prevent unused import removal
Browse files Browse the repository at this point in the history
Using feature added to the language server in v0.7.0

Resolves #273
  • Loading branch information
apexskier committed Jan 16, 2023
1 parent 17ca8e1 commit 1f9eaa3
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/commands/organizeImports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class MockRange {
}
(global as any).Range = MockRange;

jest.mock("../skipDestructiveOrganizeImports", () => ({
skipDestructiveOrganizeImports: () => false,
}));

describe("organizeImports command", () => {
beforeEach(() => {
(global as any).nova = Object.assign(nova, {
Expand Down Expand Up @@ -89,7 +93,7 @@ describe("organizeImports command", () => {
2,
"workspace/executeCommand",
{
arguments: ["/path"],
arguments: ["/path", { skipDestructiveCodeActions: false }],
command: "_typescript.organizeImports",
}
);
Expand Down
6 changes: 5 additions & 1 deletion src/commands/organizeImports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as lspTypes from "vscode-languageserver-protocol";
import { wrapCommand } from "../novaUtils";
import { skipDestructiveOrganizeImports } from "../skipDestructiveOrganizeImports";

// NOTE: this is explicitly built for the typescript-language-server; it directly invokes the specific command it uses.
// In order to decouple and become LSP generic, we'd need to first send a code action request for only
Expand Down Expand Up @@ -51,7 +52,10 @@ export function registerOrganizeImports(client: LanguageClient) {

const organizeImportsCommand: lspTypes.ExecuteCommandParams = {
command: "_typescript.organizeImports",
arguments: [editor.document.path],
arguments: [
editor.document.path,
{ skipDestructiveCodeActions: skipDestructiveOrganizeImports() },
],
};
await client.sendRequest(
"workspace/executeCommand",
Expand Down
3 changes: 3 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ jest.mock("./tsUserPreferences", () => ({
jest.mock("./isEnabledForJavascript", () => ({
isEnabledForJavascript: () => true,
}));
jest.mock("./skipDestructiveOrganizeImports", () => ({
skipDestructiveOrganizeImports: () => false,
}));
jest.mock("nova-extension-utils");

jest.useFakeTimers();
Expand Down
81 changes: 81 additions & 0 deletions src/skipDestructiveOrganizeImports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(global as any).nova = Object.assign(nova, {
commands: {
invoke: jest.fn(),
},
config: {
onDidChange: jest.fn(),
["get"]: jest.fn(),
},
workspace: {
config: { onDidChange: jest.fn(), ["get"]: jest.fn() },
},
});

describe("skipDestructiveOrganizeImports", () => {
beforeEach(() => {
(nova.workspace.config.get as jest.Mock).mockReset();
(nova.config.get as jest.Mock).mockReset();
});

const {
skipDestructiveOrganizeImports,
} = require("./skipDestructiveOrganizeImports");

describe("reloads extension when it changes", () => {
it("globally and for the workspace", () => {
expect(nova.config.onDidChange).toBeCalledTimes(1);
expect(nova.config.onDidChange).toBeCalledWith(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
expect.any(Function)
);
expect(nova.workspace.config.onDidChange).toBeCalledTimes(1);
expect(nova.workspace.config.onDidChange).toBeCalledWith(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
expect.any(Function)
);
// same function
const onWorkspaceChange = (nova.workspace.config.onDidChange as jest.Mock)
.mock.calls[0][1];
const onGlobalChange = (nova.config.onDidChange as jest.Mock).mock
.calls[0][1];
expect(onWorkspaceChange).toBe(onGlobalChange);
});

it("by calling the reload command", () => {
const reload = (nova.config.onDidChange as jest.Mock).mock.calls[0][1];
reload();
expect(nova.commands.invoke).toBeCalledTimes(1);
expect(nova.commands.invoke).toBeCalledWith(
"apexskier.typescript.reload"
);
});
});

describe("is true by default", () => {
expect(skipDestructiveOrganizeImports()).toBe(true);
});

describe("can be disabled globally", () => {
(nova.workspace.config.get as jest.Mock).mockReturnValueOnce(null);
(nova.config.get as jest.Mock).mockReturnValueOnce(false);
expect(skipDestructiveOrganizeImports()).toBe(false);
});

describe("can be enabled globally", () => {
(nova.workspace.config.get as jest.Mock).mockReturnValueOnce(null);
(nova.config.get as jest.Mock).mockReturnValueOnce(true);
expect(skipDestructiveOrganizeImports()).toBe(true);
});

describe("can be disabled in the workspace", () => {
(nova.workspace.config.get as jest.Mock).mockReturnValueOnce("False");
(nova.config.get as jest.Mock).mockReturnValueOnce(true);
expect(skipDestructiveOrganizeImports()).toBe(false);
});

describe("can be enabled in the workspace", () => {
(nova.workspace.config.get as jest.Mock).mockReturnValueOnce("True");
(nova.config.get as jest.Mock).mockReturnValueOnce(false);
expect(skipDestructiveOrganizeImports()).toBe(true);
});
});
37 changes: 37 additions & 0 deletions src/skipDestructiveOrganizeImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function reload() {
nova.commands.invoke("apexskier.typescript.reload");
}
nova.config.onDidChange(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
reload
);
nova.workspace.config.onDidChange(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
reload
);

function getWorkspaceSetting(): boolean | null {
const str = nova.workspace.config.get(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
"string"
);
switch (str) {
case "False":
return false;
case "True":
return true;
default:
return null;
}
}

export function skipDestructiveOrganizeImports(): boolean {
return (
getWorkspaceSetting() ??
nova.config.get(
"apexskier.typescript.config.skipDestructiveOrganizeImports",
"boolean"
) ??
true
);
}
13 changes: 13 additions & 0 deletions typescript.novaextension/extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
"type": "boolean",
"default": true
},
{
"key": "apexskier.typescript.config.skipDestructiveOrganizeImports",
"title": "Skip destructive organize imports changes",
"type": "boolean",
"default": false
},
{
"title": "TypeScript server User Preferences",
"description": "Advanced configuration passed to the underlying typescript server. These may not apply to older versions of TypeScript.",
Expand Down Expand Up @@ -352,6 +358,13 @@
"values": ["Inherit from Global Settings", "Disable", "Enable"],
"default": "Inherit from Global Settings"
},
{
"key": "apexskier.typescript.config.skipDestructiveOrganizeImports",
"title": "Skip destructive organize imports changes",
"type": "enum",
"values": ["Inherit from Global Settings", "False", "True"],
"default": "Inherit from Global Settings"
},
{
"title": "TypeScript server User Preferences",
"description": "Advanced configuration passed to the underlying typescript server. These may not apply to older versions of TypeScript.",
Expand Down

0 comments on commit 1f9eaa3

Please sign in to comment.