Skip to content

Commit

Permalink
Fixes .prettierignore and .editorconfig not working (#832)
Browse files Browse the repository at this point in the history
* chore: changeset

* nit: add a warning message when failing to load the config

* nit: also log it to the console

* chore: changeset
  • Loading branch information
Princesseuh authored Mar 15, 2024
1 parent 97c422b commit c1fa115
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .changeset/orange-schools-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@astrojs/language-server": patch
"astro-vscode": patch
---

Fixes `.prettierignore` and `.editorconfig` not working correctly. This update also improves the error logging around Prettier, the LSP will now warn when it failed to load the Prettier config.
30 changes: 15 additions & 15 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = false

[{.*,*.md,*.json,*.toml,*.yml,*.yaml}]
indent_style = space
# https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = false

[{.*,*.md,*.json,*.toml,*.yml,*.yaml}]
indent_style = space
12 changes: 6 additions & 6 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"@volar/language-service": "~2.1.2",
"@volar/typescript": "~2.1.2",
"fast-glob": "^3.2.12",
"volar-service-css": "0.0.33",
"volar-service-emmet": "0.0.33",
"volar-service-html": "0.0.33",
"volar-service-prettier": "0.0.33",
"volar-service-typescript": "0.0.33",
"volar-service-typescript-twoslash-queries": "0.0.33",
"volar-service-css": "0.0.34",
"volar-service-emmet": "0.0.34",
"volar-service-html": "0.0.34",
"volar-service-prettier": "0.0.34",
"volar-service-typescript": "0.0.34",
"volar-service-typescript-twoslash-queries": "0.0.34",
"vscode-html-languageservice": "^5.1.2",
"vscode-uri": "^3.0.8"
},
Expand Down
27 changes: 21 additions & 6 deletions packages/language-server/src/languageServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,28 @@ export function createServerOptions(
},
{
documentSelector: ['astro'],
getFormattingOptions: async (prettier, document, formatOptions, context) => {
getFormattingOptions: async (prettierInstance, document, formatOptions, context) => {
const filePath = URI.parse(document.uri).fsPath;
const configOptions = await prettier.resolveConfig(filePath, {
// This seems to be broken since Prettier 3, and it'll always use its cumbersome cache. Hopefully it works one day.
useCache: false,
});
const editorOptions = await context.env.getConfiguration<{}>?.('prettier', document.uri);

let configOptions = null;
try {
configOptions = await prettierInstance.resolveConfig(filePath, {
// This seems to be broken since Prettier 3, and it'll always use its cumbersome cache. Hopefully it works one day.
useCache: false,
editorconfig: true,
});
} catch (e) {
connection.sendNotification(ShowMessageNotification.type, {
message: `Failed to load Prettier config.\n\nError:\n${e}`,
type: MessageType.Warning,
});
console.error('Failed to load Prettier config.', e)
}

const editorOptions = await context.env.getConfiguration<object>?.(
'prettier',
document.uri
);

// Return a config with the following cascade:
// - Prettier config file should always win if it exists, if it doesn't:
Expand Down
4 changes: 4 additions & 0 deletions packages/language-server/test/fixture/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*]
end_of_line = crlf
1 change: 1 addition & 0 deletions packages/language-server/test/fixture/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dontFormat.astro
11 changes: 11 additions & 0 deletions packages/language-server/test/fixture/dontFormat.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---

<div>


Hello</div>
3 changes: 3 additions & 0 deletions packages/language-server/test/fixture/editorConfig.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<div></div>
</div>
25 changes: 0 additions & 25 deletions packages/language-server/test/misc/format.test.ts

This file was deleted.

51 changes: 51 additions & 0 deletions packages/language-server/test/misc/prettier-format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as path from 'node:path';
import { Range } from '@volar/language-server';
import { expect } from 'chai';
import { describe } from 'mocha';
import { type LanguageServer, getLanguageServer } from '../server.js';

describe('Formatting - Prettier', () => {
let languageServer: LanguageServer;

before(async () => (languageServer = await getLanguageServer()));

it('Can format document', async () => {
const document = await languageServer.openFakeDocument(`---\n\n\n---`, 'astro');
const formatEdits = await languageServer.handle.sendDocumentFormattingRequest(document.uri, {
tabSize: 2,
insertSpaces: true,
});

expect(formatEdits).to.deep.equal([
{
range: Range.create(0, 0, 3, 3),
newText: '---\n\n---\n',
},
]);
});

it('Can ignore documents correctly', async () => {
const document = await languageServer.handle.openTextDocument(path.resolve(__dirname, '..', 'fixture', 'dontFormat.astro'), 'astro');
const formatEdits = await languageServer.handle.sendDocumentFormattingRequest(document.uri, {
tabSize: 2,
insertSpaces: true,
});

expect(formatEdits).to.deep.equal(null);
});

it('Respect .editorconfig', async () => {
const document = await languageServer.handle.openTextDocument(path.resolve(__dirname, '..', 'fixture', 'editorConfig.astro'), 'astro');
const formatEdits = await languageServer.handle.sendDocumentFormattingRequest(document.uri, {
tabSize: 2,
insertSpaces: true,
});

expect(formatEdits).to.deep.equal([
{
range: Range.create(0, 0, 3, 0),
newText: '<div>\r\n\t<div></div>\r\n</div>\r\n'
}
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import * as path from 'node:path';
import {
type Diagnostic,
DiagnosticSeverity,
Expand Down
61 changes: 34 additions & 27 deletions pnpm-lock.yaml

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

0 comments on commit c1fa115

Please sign in to comment.