From e262e7dd538833970065a12bfdeb378305a43f45 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 12:51:07 +0000 Subject: [PATCH 1/4] Add githubCodeOwners.format.style to format like before 5.1.0 Setting it to legacy asks the gh-codeowners language server to line up the first owner of every rule, offset by githubCodeOwners.format.alignmentOffset, which is no longer deprecated. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018s9fHcEQxmXZ7pQ6v9W2KM --- CHANGELOG.md | 8 +++++++ README.md | 2 ++ package.json | 30 ++++++++++++++++------- src/configuration.ts | 23 ++++++++++++++++++ src/language-server.ts | 12 ++++++++++ test/suite/language-server.test.ts | 38 ++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff3a1d..fa2a2b8 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.style`. Set it to `legacy` to format like before 5.1.0, lining up the first owner of every rule, offset from the longest pattern by `githubCodeOwners.format.alignmentOffset`. + +### Changed + +- `githubCodeOwners.format.alignmentOffset` is no longer deprecated. It sets the offset for the `legacy` style. + ## 5.1.0 - 2026-11-08 ### Changed diff --git a/README.md b/README.md index 6dbc697..fa38a4e 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.style` to `legacy`. The first owner of every rule in the file lines up, offset from the longest pattern by `githubCodeOwners.format.alignmentOffset`, and nothing else changes. + owners lined up in each section of a CODEOWNERS file ### Command diff --git a/package.json b/package.json index 439f5f2..7e2d620 100644 --- a/package.json +++ b/package.json @@ -68,17 +68,29 @@ "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.style#` to `legacy`.", + "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.style to legacy." }, "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." + "markdownDescription": "With `#githubCodeOwners.format.style#` set to `legacy`, space offset to use from the longest file pattern to the first code owner when aligning." + }, + "githubCodeOwners.format.style": { + "type": "string", + "default": "default", + "order": 1, + "enum": [ + "default", + "legacy" + ], + "markdownEnumDescriptions": [ + "Line up owners and trailing comments in each section, one space after the longest pattern.", + "Line up the first owner of every rule in the file, offset from the longest pattern by `#githubCodeOwners.format.alignmentOffset#`, and change nothing else, like before 5.1.0." + ], + "markdownDescription": "How Format Document formats CODEOWNERS." }, "githubCodeOwners.fileCounts.enabled": { "order": 6, @@ -156,15 +168,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.style#` to `legacy`.", + "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.style to legacy.", "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.alignmentOffset#` instead.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.alignmentOffset instead.", "type": "number", "default": 4, "minimum": 1, diff --git a/src/configuration.ts b/src/configuration.ts index f0e9e9f..9d301a0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -62,3 +62,26 @@ export function isLanguageServerEnabled(): boolean { false ) } + +export const FORMAT = "githubCodeOwners.format" + +/** Settings for the language server, from the extension's settings. */ +export type ServerSettings = { + format: { style: "default" | "legacy"; alignmentOffset: number } +} + +export function getServerSettings(): ServerSettings { + const config = vscode.workspace.getConfiguration(FORMAT) + const alignmentOffset = config.get("alignmentOffset") + return { + format: { + style: config.get("style") === "legacy" ? "legacy" : "default", + alignmentOffset: + typeof alignmentOffset === "number" && + Number.isInteger(alignmentOffset) && + alignmentOffset >= 0 + ? alignmentOffset + : 4, + }, + } +} diff --git a/src/language-server.ts b/src/language-server.ts index 29964aa..8a56faf 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, + FORMAT, 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 (e.affectsConfiguration(FORMAT)) { + 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..50870f0 100644 --- a/test/suite/language-server.test.ts +++ b/test/suite/language-server.test.ts @@ -124,6 +124,44 @@ suite("language server", function () { ) }) + test("formats in the legacy style when it's set", async () => { + const config = vscode.workspace.getConfiguration("githubCodeOwners") + await config.update( + "format.style", + "legacy", + vscode.ConfigurationTarget.Global, + ) + try { + const document = await vscode.workspace.openTextDocument({ + language: "codeowners", + content: " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n", + }) + const edits = await waitFor( + () => + vscode.commands.executeCommand( + "vscode.executeFormatDocumentProvider", + document.uri, + { tabSize: 2, insertSpaces: true }, + ), + (e) => e != null && e.length > 0, + ) + const edit = new vscode.WorkspaceEdit() + edit.set(document.uri, edits ?? []) + await vscode.workspace.applyEdit(edit) + // Owners line up 4 spaces after the longest pattern, as before 5.1.0. + assert.strictEqual( + document.getText(), + " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n", + ) + } finally { + await config.update( + "format.style", + undefined, + vscode.ConfigurationTarget.Global, + ) + } + }) + test("hides file counts when they're disabled", async () => { const config = vscode.workspace.getConfiguration("githubCodeOwners") await config.update( From 2d8f306b59e966a246d569e65302f99ac0d4470f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 00:05:43 +0000 Subject: [PATCH 2/4] Replace githubCodeOwners.format.style with align and spacing Match gh-codeowners' fmt --align group|file and --spacing. The deprecated alignmentOffset settings still apply, as spacing one more than the offset, until spacing is set. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018s9fHcEQxmXZ7pQ6v9W2KM --- CHANGELOG.md | 4 +- README.md | 2 +- package.json | 48 ++++++++++++-------- src/configuration.ts | 54 ++++++++++++++++++---- src/language-server.ts | 4 +- test/suite/language-server.test.ts | 73 +++++++++++++++++------------- 6 files changed, 118 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2a2b8..46d24cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,11 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add `githubCodeOwners.format.style`. Set it to `legacy` to format like before 5.1.0, lining up the first owner of every rule, offset from the longest pattern by `githubCodeOwners.format.alignmentOffset`. +- 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` is no longer deprecated. It sets the offset for the `legacy` style. +- `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 diff --git a/README.md b/README.md index fa38a4e..503a274 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ 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.style` to `legacy`. The first owner of every rule in the file lines up, offset from the longest pattern by `githubCodeOwners.format.alignmentOffset`, and nothing else changes. +> 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 diff --git a/package.json b/package.json index 7e2d620..9899501 100644 --- a/package.json +++ b/package.json @@ -68,29 +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#`. To format like before, set `#githubCodeOwners.format.style#` to `legacy`.", - "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.style to legacy." + "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.alignmentOffset": { - "type": "number", - "default": 4, - "minimum": 1, - "order": 2, - "markdownDescription": "With `#githubCodeOwners.format.style#` set to `legacy`, space offset to use from the longest file pattern to the first code owner when aligning." - }, - "githubCodeOwners.format.style": { + "githubCodeOwners.format.align": { "type": "string", - "default": "default", + "default": "group", "order": 1, "enum": [ - "default", - "legacy" + "group", + "file" ], "markdownEnumDescriptions": [ - "Line up owners and trailing comments in each section, one space after the longest pattern.", - "Line up the first owner of every rule in the file, offset from the longest pattern by `#githubCodeOwners.format.alignmentOffset#`, and change nothing else, like before 5.1.0." + "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": "How Format Document formats CODEOWNERS." + "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, + "description": "Space offset to use from the longest file pattern the first code owner when aligning.", + "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, @@ -168,15 +176,15 @@ } }, "github-code-owners.format.enabled": { - "markdownDeprecationMessage": "**Deprecated**: Formatting is always available from the language server. Use `Format Document` or `#editor.formatOnSave#`. To format like before, set `#githubCodeOwners.format.style#` to `legacy`.", - "deprecationMessage": "Deprecated: Formatting is always available from the language server. Use Format Document or editor.formatOnSave. To format like before, set githubCodeOwners.format.style to legacy.", + "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**: Please use `#githubCodeOwners.format.alignmentOffset#` instead.", - "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.alignmentOffset instead.", + "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/src/configuration.ts b/src/configuration.ts index 9d301a0..49ecf79 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -64,24 +64,58 @@ export function isLanguageServerEnabled(): boolean { } 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: { style: "default" | "legacy"; alignmentOffset: number } + 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 config = vscode.workspace.getConfiguration(FORMAT) - const alignmentOffset = config.get("alignmentOffset") + const align = vscode.workspace.getConfiguration(FORMAT).get("align") return { format: { - style: config.get("style") === "legacy" ? "legacy" : "default", - alignmentOffset: - typeof alignmentOffset === "number" && - Number.isInteger(alignmentOffset) && - alignmentOffset >= 0 - ? alignmentOffset - : 4, + 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 8a56faf..ae9a72b 100644 --- a/src/language-server.ts +++ b/src/language-server.ts @@ -11,7 +11,7 @@ import { import { FILE_COUNTS_ENABLED, - FORMAT, + affectsServerSettings, getLanguageServerPath, getServerSettings, isFileCountsEnabled, @@ -109,7 +109,7 @@ export class CodeownersLanguageServer { context.subscriptions.push( client, vscode.workspace.onDidChangeConfiguration((e) => { - if (e.affectsConfiguration(FORMAT)) { + if (affectsServerSettings(e)) { void client.sendNotification( DidChangeConfigurationNotification.type, { diff --git a/test/suite/language-server.test.ts b/test/suite/language-server.test.ts index 50870f0..9265d9d 100644 --- a/test/suite/language-server.test.ts +++ b/test/suite/language-server.test.ts @@ -124,41 +124,50 @@ suite("language server", function () { ) }) - test("formats in the legacy style when it's set", async () => { + 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") - await config.update( - "format.style", - "legacy", - vscode.ConfigurationTarget.Global, - ) + const global = vscode.ConfigurationTarget.Global try { - const document = await vscode.workspace.openTextDocument({ - language: "codeowners", - content: " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n", - }) - const edits = await waitFor( - () => - vscode.commands.executeCommand( - "vscode.executeFormatDocumentProvider", - document.uri, - { tabSize: 2, insertSpaces: true }, - ), - (e) => e != null && e.length > 0, - ) - const edit = new vscode.WorkspaceEdit() - edit.set(document.uri, edits ?? []) - await vscode.workspace.applyEdit(edit) - // Owners line up 4 spaces after the longest pattern, as before 5.1.0. - assert.strictEqual( - document.getText(), - " /src/ @acme/web @acme/x # web\n/docs/ @acme/docs\n", - ) + // 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 { - await config.update( - "format.style", - undefined, - vscode.ConfigurationTarget.Global, - ) + for (const key of [ + "format.align", + "format.alignmentOffset", + "format.spacing", + ]) { + await config.update(key, undefined, global) + } } }) From 2ee9f2c7be38cf1daf3a3e81311678533747ef2f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 02:29:56 +0000 Subject: [PATCH 3/4] Wait for the language server's quick fix, not any code action VS Code's own AI actions can answer first, so the test sometimes saw only those. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018s9fHcEQxmXZ7pQ6v9W2KM --- test/suite/language-server.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/suite/language-server.test.ts b/test/suite/language-server.test.ts index 9265d9d..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), From def379351dd9f1aea7409af1746fbac840173711 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 12:47:33 +0000 Subject: [PATCH 4/4] Bundle gh-codeowners v0.4.0 It reads the format settings this extension sends. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018s9fHcEQxmXZ7pQ6v9W2KM --- s/download-server | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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