Skip to content

Adds selection range provider #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2023
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
2 changes: 2 additions & 0 deletions server/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import FoldingProvider from "./folding";
import FormattingProvider from "./formatting";
import LinkedEditProvider from "./linkedEdit";
import LinkProvider from "./link";
import SelectionRangeProvider from "./range";
import ValidationProvider from "./validation";
import Watch from "./watch";

Expand All @@ -16,6 +17,7 @@ export {
FormattingProvider,
LinkedEditProvider,
LinkProvider,
SelectionRangeProvider,
ValidationProvider,
Watch,
};
98 changes: 98 additions & 0 deletions server/plugins/range.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as LSP from "vscode-languageserver/node";
import * as Markdoc from "@markdoc/markdoc";
import * as assert from "node:assert";
import { test } from "node:test";
import SelectionRangeProvider from "./range";

const example1 = `
# Example 1

{% foo %}
test
{% /foo %}
`;

const example2 = `
# Example 2

{% foo %}
{% bar %}
test

{% baz %}
test
{% /baz %}
{% /bar %}

test
{% /foo %}
`;

const connectionMock = { onSelectionRanges(...args) {} };

test("folding provider", async (t) => {
// @ts-expect-error
const provider = new SelectionRangeProvider({}, connectionMock, {});

await t.test("simple example", () => {
const ast = Markdoc.parse(example1);
const range = provider.findSelectionRange(ast, LSP.Position.create(4, 3));
assert.deepEqual(range, {
parent: {
parent: undefined,
range: {
start: { line: 3, character: 0 },
end: { line: 6, character: 0 },
},
},
range: {
start: { line: 4, character: 0 },
end: { line: 5, character: 0 },
},
});
});

await t.test("nested example", () => {
const ast = Markdoc.parse(example2);
const range = provider.findSelectionRange(ast, LSP.Position.create(8, 3));
const expected = {
parent: {
parent: {
parent: {
parent: {
parent: {
parent: undefined,
range: {
start: { line: 3, character: 0 },
end: { line: 14, character: 0 },
},
},
range: {
start: { line: 4, character: 0 },
end: { line: 13, character: 0 },
},
},
range: {
start: { line: 4, character: 0 },
end: { line: 11, character: 0 },
},
},
range: {
start: { line: 5, character: 0 },
end: { line: 10, character: 0 },
},
},
range: {
start: { line: 7, character: 0 },
end: { line: 10, character: 0 },
},
},
range: {
start: { line: 8, character: 0 },
end: { line: 9, character: 0 },
},
};

assert.deepEqual(range, expected);
});
});
46 changes: 46 additions & 0 deletions server/plugins/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as LSP from "vscode-languageserver/node";
import * as utils from "../utils";
import type { Config, ServiceInstances } from "../types";
import type * as Markdoc from "@markdoc/markdoc";

export default class SelectionRangeProvider {
constructor(
protected config: Config,
protected connection: LSP.Connection,
protected services: ServiceInstances
) {
connection.onSelectionRanges(this.onSelectionRange.bind(this));
}

register(registration: LSP.BulkRegistration) {
registration.add(LSP.SelectionRangeRequest.type, {
documentSelector: null,
});
}

findSelectionRange(ast: Markdoc.Node, position: LSP.Position) {
let currentRange: LSP.SelectionRange | undefined;
for (const range of utils.getBlockRanges(ast)) {
if (range.start > position.line) break;
if (range.end > position.line) {
currentRange = {
range: LSP.Range.create(range.start + 1, 0, range.end, 0),
parent: {
range: LSP.Range.create(range.start, 0, range.end + 1, 0),
parent: currentRange,
},
};
}
}

return currentRange ?? { range: LSP.Range.create(position, position) };
}

onSelectionRange({ textDocument, positions }: LSP.SelectionRangeParams) {
const ast = this.services.Documents.ast(textDocument.uri);
if (ast)
return positions.map((position) =>
this.findSelectionRange(ast, position)
);
}
}
3 changes: 2 additions & 1 deletion server/plugins/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ export default class ValidationProvider {
} = err;

return {
code: error.id,
range: this.createRange(line, error.location ?? location),
severity: this.severity(error.level),
message: error.message,
source: `markdoc ${this.config.id ?? ""}`,
source: `markdoc:${this.config.id ?? ""}`,
};
}

Expand Down