Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<img src="./images/formatting.png" alt="owners lined up in each section of a CODEOWNERS file" height="193px"/>

### Command
Expand Down
38 changes: 29 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion s/download-server
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
}
12 changes: 12 additions & 0 deletions src/language-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs"
import vscode from "vscode"
import {
DidChangeConfigurationNotification,
InlayHintRequest,
LanguageClient,
TransportKind,
Expand All @@ -10,7 +11,9 @@ import {

import {
FILE_COUNTS_ENABLED,
affectsServerSettings,
getLanguageServerPath,
getServerSettings,
isFileCountsEnabled,
isLanguageServerEnabled,
} from "./configuration"
Expand Down Expand Up @@ -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) : [],
Expand All @@ -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
}
Expand Down
52 changes: 50 additions & 2 deletions test/suite/language-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
Loading