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

Commit e8e725e

Browse files
authored
Configure document formatting before organizing imports (#241)
* Configure document formatting before organizing imports Fixes #236 * Lint fix * Lint * tests
1 parent 9565d31 commit e8e725e

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Turn on extension development in Nova in Preferences > General > Extension Devel
1616

1717
To debug the underlying language server, modify the `run.sh` file to use the [`--inspect` flag](https://nodejs.org/en/docs/guides/debugging-getting-started/) and use [your preferred inspector to debug](https://nodejs.org/en/docs/guides/debugging-getting-started/#inspector-clients).
1818

19+
To debug the underlying `tsserver` modify the language server code to exec it through `node` with [`--inspect`](https://nodejs.org/en/docs/guides/debugging-getting-started/). The file to be modified is `~/Library/Application Support/Nova/Extensions/apexskier.typescript/dependencyManagement/node_modules/typescript-language-server/lib/tsp-client.js`. (Set `args = ['node', '--inspect-brk', tsserverPath]` and replace `tsserverPath` with `'/usr/bin/env'` in `cp.fork`/`cp.spawn`.) You can increase server shutdown timeouts in the file `~/Library/Application Support/Nova/Extensions/apexskier.typescript/dependencyManagement/node_modules/typescript-language-server/lib/utils.js`
20+
1921
Use the Extension Console in Nova to debug the extension. I haven't found a way to get a debugger attached to the JavaScriptCore context.
2022

2123
### Extension dependencies

src/commands/organizeImports.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ describe("organizeImports command", () => {
3838
},
3939
eol: "\n",
4040
},
41+
softTabs: true,
42+
tabLength: 2,
4143
edit: jest.fn(),
4244
selectWordsContainingCursors: jest.fn(),
4345
scrollToCursorPosition: jest.fn(),
@@ -59,7 +61,7 @@ describe("organizeImports command", () => {
5961
return command;
6062
}
6163

62-
it("asks the server to organize imports, then resets your selection", async () => {
64+
it("configures formatting with the server, asks the server to organize imports, then resets your selection", async () => {
6365
const mockLanguageClient = {
6466
sendRequest: jest.fn().mockImplementationOnce(() => {
6567
mockEditor.document.length = 14;
@@ -72,8 +74,20 @@ describe("organizeImports command", () => {
7274
expect(mockEditor.selectedRanges).toEqual([new Range(2, 3)]);
7375
await command(mockEditor);
7476

75-
expect(mockLanguageClient.sendRequest).toBeCalledTimes(1);
76-
expect(mockLanguageClient.sendRequest).toHaveBeenCalledWith(
77+
expect(mockLanguageClient.sendRequest).toBeCalledTimes(2);
78+
expect(mockLanguageClient.sendRequest).toHaveBeenNthCalledWith(
79+
1,
80+
"textDocument/formatting",
81+
{
82+
textDocument: { uri: "currentDocURI" },
83+
options: {
84+
insertSpaces: true,
85+
tabSize: 2,
86+
},
87+
}
88+
);
89+
expect(mockLanguageClient.sendRequest).toHaveBeenNthCalledWith(
90+
2,
7791
"workspace/executeCommand",
7892
{
7993
arguments: ["/path"],

src/commands/organizeImports.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export function registerOrganizeImports(client: LanguageClient) {
3838
return;
3939
}
4040

41+
// Ensure the language server is aware of the formatting settings for this editor
42+
// Normally this command is used to apply formatting, but we just skip applying
43+
// the response and rely on the server caching the formatting settings.
44+
const documentFormatting: lspTypes.DocumentFormattingParams = {
45+
textDocument: { uri: editor.document.uri },
46+
options: {
47+
insertSpaces: editor.softTabs,
48+
tabSize: editor.tabLength,
49+
},
50+
};
51+
await client.sendRequest("textDocument/formatting", documentFormatting);
52+
4153
const organizeImportsCommand: lspTypes.ExecuteCommandParams = {
4254
command: "_typescript.organizeImports",
4355
arguments: [editor.document.path],

src/searchResults.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import type * as lspTypes from "vscode-languageserver-protocol";
33
import * as lsp from "vscode-languageserver-types";
44
import {
5-
createSymbolSearchResultsTree,
65
createLocationSearchResultsTree,
6+
createSymbolSearchResultsTree,
77
} from "./searchResults";
88

99
(global as any).nova = Object.assign(nova, {

0 commit comments

Comments
 (0)