diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff3a1d..46d24cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `githubCodeOwners.format.align` and `githubCodeOwners.format.spacing`. Set them to `file` and `5` to format like before 5.1.0 (#44). + +### Changed + +- `githubCodeOwners.format.alignmentOffset` sets the spacing to the offset plus 1, when `githubCodeOwners.format.spacing` isn't set (#44). + ## 5.1.0 - 2026-11-08 ### Changed diff --git a/README.md b/README.md index 6dbc697..503a274 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Escapes, wildcards, comments and owners are highlighted the way GitHub reads the Format Document lines up owners and trailing comments in each section. +> To format like before 5.1.0, set `githubCodeOwners.format.align` to `file` and `githubCodeOwners.format.spacing` to `5`. + owners lined up in each section of a CODEOWNERS file ### Command diff --git a/package.json b/package.json index 439f5f2..9899501 100644 --- a/package.json +++ b/package.json @@ -68,17 +68,37 @@ "default": false, "order": 1, "description": "Whether or not to enable formatting.", - "markdownDeprecationMessage": "**Deprecated**: Formatting is always available from the language server. Use `Format Document` or `#editor.formatOnSave#`.", - "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave." + "markdownDeprecationMessage": "**Deprecated**: Formatting is always available from the language server. Use `Format Document` or `#editor.formatOnSave#`. To format like before, set `#githubCodeOwners.format.align#` to `file` and `#githubCodeOwners.format.spacing#` to `5`.", + "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.align to file and githubCodeOwners.format.spacing to 5." + }, + "githubCodeOwners.format.align": { + "type": "string", + "default": "group", + "order": 1, + "enum": [ + "group", + "file" + ], + "markdownEnumDescriptions": [ + "Line up owners and trailing comments in each group of consecutive rules.", + "Line up the first owner of every rule in the file, and change nothing else, like before 5.1.0." + ], + "markdownDescription": "Which rules' owners Format Document lines up." + }, + "githubCodeOwners.format.spacing": { + "type": "integer", + "default": 1, + "minimum": 1, + "order": 2, + "markdownDescription": "Spaces between the longest pattern and the owners. Formatting like before 5.1.0 used 5." }, "githubCodeOwners.format.alignmentOffset": { "type": "number", "default": 4, "minimum": 1, - "order": 2, "description": "Space offset to use from the longest file pattern the first code owner when aligning.", - "markdownDeprecationMessage": "**Deprecated**: The language server lines up owners one space after the longest pattern in each section.", - "deprecationMessage": "Deprecated: The language server lines up owners one space after the longest pattern in each section." + "markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.spacing#` instead, which is this offset plus 1.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.spacing instead, which is this offset plus 1." }, "githubCodeOwners.fileCounts.enabled": { "order": 6, @@ -156,15 +176,15 @@ } }, "github-code-owners.format.enabled": { - "markdownDeprecationMessage": "**Deprecated**: Formatting is always available from the language server. Use `Format Document` or `#editor.formatOnSave#`.", - "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave.", + "markdownDeprecationMessage": "**Deprecated**: Formatting is always available from the language server. Use `Format Document` or `#editor.formatOnSave#`. To format like before, set `#githubCodeOwners.format.align#` to `file` and `#githubCodeOwners.format.spacing#` to `5`.", + "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.align to file and githubCodeOwners.format.spacing to 5.", "type": "boolean", "default": false, "description": "Whether or not to enable formatting." }, "github-code-owners.format.alignment-offset": { - "markdownDeprecationMessage": "**Deprecated**: The language server lines up owners one space after the longest pattern in each section.", - "deprecationMessage": "Deprecated: The language server lines up owners one space after the longest pattern in each section.", + "markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.spacing#` instead, which is this offset plus 1.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.spacing instead, which is this offset plus 1.", "type": "number", "default": 4, "minimum": 1, diff --git a/s/download-server b/s/download-server index 5f77adf..e4b9d68 100755 --- a/s/download-server +++ b/s/download-server @@ -5,7 +5,7 @@ # darwin-arm64, into bin/, where the extension looks for its language server. set -euo pipefail -GH_CODEOWNERS_VERSION=v0.3.0 +GH_CODEOWNERS_VERSION=v0.4.0 target=$1 case "$target" in diff --git a/src/configuration.ts b/src/configuration.ts index f0e9e9f..49ecf79 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -62,3 +62,60 @@ export function isLanguageServerEnabled(): boolean { false ) } + +export const FORMAT = "githubCodeOwners.format" +const DEPRECATED_ALIGNMENT_OFFSETS = [ + "githubCodeOwners.format.alignmentOffset", + "github-code-owners.format.alignment-offset", +] + +/** Settings for the language server, from the extension's settings. */ +export type ServerSettings = { + format: { align: "group" | "file"; spacing: number } +} + +/** The setting's value, if the user set it anywhere. */ +function userValue(key: string): unknown { + const value = vscode.workspace.getConfiguration().inspect(key) + return ( + value?.workspaceFolderValue ?? value?.workspaceValue ?? value?.globalValue + ) +} + +function isSpacing(value: unknown): value is number { + return typeof value === "number" && Number.isInteger(value) && value >= 1 +} + +function getSpacing(): number { + const spacing = userValue(`${FORMAT}.spacing`) + if (isSpacing(spacing)) { + return spacing + } + // The deprecated offset was one less than the spaces it made. + for (const key of DEPRECATED_ALIGNMENT_OFFSETS) { + const offset = userValue(key) + if (isSpacing(offset)) { + return offset + 1 + } + } + return 1 +} + +export function getServerSettings(): ServerSettings { + const align = vscode.workspace.getConfiguration(FORMAT).get("align") + return { + format: { + align: align === "file" ? "file" : "group", + spacing: getSpacing(), + }, + } +} + +export function affectsServerSettings( + e: vscode.ConfigurationChangeEvent, +): boolean { + return ( + e.affectsConfiguration(FORMAT) || + DEPRECATED_ALIGNMENT_OFFSETS.some((key) => e.affectsConfiguration(key)) + ) +} diff --git a/src/language-server.ts b/src/language-server.ts index 29964aa..ae9a72b 100644 --- a/src/language-server.ts +++ b/src/language-server.ts @@ -1,6 +1,7 @@ import fs from "fs" import vscode from "vscode" import { + DidChangeConfigurationNotification, InlayHintRequest, LanguageClient, TransportKind, @@ -10,7 +11,9 @@ import { import { FILE_COUNTS_ENABLED, + affectsServerSettings, getLanguageServerPath, + getServerSettings, isFileCountsEnabled, isLanguageServerEnabled, } from "./configuration" @@ -83,6 +86,7 @@ export class CodeownersLanguageServer { { language: "codeowners", scheme: "untitled" }, ], outputChannel, + initializationOptions: getServerSettings(), middleware: { provideInlayHints: (document, viewPort, token, next) => isFileCountsEnabled() ? next(document, viewPort, token) : [], @@ -105,6 +109,14 @@ export class CodeownersLanguageServer { context.subscriptions.push( client, vscode.workspace.onDidChangeConfiguration((e) => { + if (affectsServerSettings(e)) { + void client.sendNotification( + DidChangeConfigurationNotification.type, + { + settings: getServerSettings(), + }, + ) + } if (!e.affectsConfiguration(FILE_COUNTS_ENABLED)) { return } diff --git a/test/suite/language-server.test.ts b/test/suite/language-server.test.ts index cfe6ae2..08c896a 100644 --- a/test/suite/language-server.test.ts +++ b/test/suite/language-server.test.ts @@ -89,9 +89,10 @@ suite("language server", function () { new vscode.Range(2, 1, 2, 1), vscode.CodeActionKind.QuickFix.value, ), - (a) => a.length > 0, + // VS Code adds its own AI actions, which have no edit, and can + // answer before the language server does. + (a) => a.some((action) => action.edit != null), ) - // VS Code adds its own AI actions, which have no edit. const fixes = actions.filter((a) => a.edit != null) assert.deepStrictEqual( fixes.map((a) => a.title), @@ -124,6 +125,53 @@ suite("language server", function () { ) }) + test("formats with the alignment and spacing from the settings", async () => { + const content = " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n" + // `content` formatted, waiting for the server to get new settings. + const formatted = (expected: string) => + waitFor( + async () => { + const document = await vscode.workspace.openTextDocument({ + language: "codeowners", + content, + }) + const edits = await vscode.commands.executeCommand< + vscode.TextEdit[] | undefined + >("vscode.executeFormatDocumentProvider", document.uri, { + tabSize: 2, + insertSpaces: true, + }) + const edit = new vscode.WorkspaceEdit() + edit.set(document.uri, edits ?? []) + await vscode.workspace.applyEdit(edit) + return document.getText() + }, + (text) => text === expected, + ) + const config = vscode.workspace.getConfiguration("githubCodeOwners") + const global = vscode.ConfigurationTarget.Global + try { + // The deprecated offset still works, as spacing one more than it. + await config.update("format.align", "file", global) + await config.update("format.alignmentOffset", 4, global) + const file5 = + " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n" + assert.strictEqual(await formatted(file5), file5) + // Spacing wins over the offset. + await config.update("format.spacing", 2, global) + const file2 = " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n" + assert.strictEqual(await formatted(file2), file2) + } finally { + for (const key of [ + "format.align", + "format.alignmentOffset", + "format.spacing", + ]) { + await config.update(key, undefined, global) + } + } + }) + test("hides file counts when they're disabled", async () => { const config = vscode.workspace.getConfiguration("githubCodeOwners") await config.update(