Skip to content

Added codeAction (extract subSchema to defs) #133

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist/
.vscode/
scratch/
TODO
.DS_Store

10 changes: 10 additions & 0 deletions language-server/package-lock.json

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

1 change: 1 addition & 0 deletions language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"keywords": [],
"dependencies": {
"chokidar": "^4.0.1",
"detect-indent": "^7.0.1",
"ignore": "^7.0.1",
"jsonc-parser": "^3.3.1",
"merge-anything": "^6.0.2",
Expand Down
2 changes: 2 additions & 0 deletions language-server/src/build-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ValidationErrorsDiagnosticsProvider } from "./features/diagnostics/vali
import { IfThenCompletionProvider } from "./features/completion/if-then-completion.js";
import { KeywordCompletionProvider } from "./features/completion/keyword-completion.js";
import { SchemaCompletionProvider } from "./features/completion/schema-completion.js";
import { ExtractSubSchemaToDefs } from "./features/codeAction/extractSubschema.js";

// Hyperjump
import { removeMediaTypePlugin } from "@hyperjump/browser";
Expand Down Expand Up @@ -49,6 +50,7 @@ export const buildServer = (connection) => {
new GotoDefinitionFeature(server, schemas);
new FindReferencesFeature(server, schemas);
new HoverFeature(server, schemas);
new ExtractSubSchemaToDefs(server, schemas, configuration);

// TODO: It's awkward that diagnostics needs a variable
const diagnostics = new DiagnosticsFeature(server, [
Expand Down
110 changes: 110 additions & 0 deletions language-server/src/features/codeAction/extractSubschema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {
CodeActionKind,
TextDocumentEdit
} from "vscode-languageserver";
import { getKeywordName } from "@hyperjump/json-schema/experimental";
import * as SchemaDocument from "../../model/schema-document.js";
import * as SchemaNode from "../../model/schema-node.js";
import { withFormatting } from "../../util/util.js";

/**
* @import { Server } from "../../services/server.js";
* @import { Schemas } from "../../services/schemas.js";
* @import { CodeAction } from "vscode-languageserver";
* @import { Configuration } from "../../services/configuration.js";
*/


export class ExtractSubSchemaToDefs {
/**
* @param {Server} server
* @param {Schemas} schemas
* @param {Configuration} configuration
*/
constructor(server, schemas, configuration) {
this.server = server;
this.schemas = schemas;
this.configuration = configuration;

server.onInitialize(() => ({
capabilities: {
codeActionProvider: true
}
}));

server.onCodeAction(async ({ textDocument, range }) => {
if (range.start.line === range.end.line && range.start.character === range.end.character) {
return [];
}

const uri = textDocument.uri;
let schemaDocument = await schemas.getOpen(uri);
if (!schemaDocument) {
return [];
}

const offset = schemaDocument.textDocument.offsetAt(range.start);
const node = SchemaDocument.findNodeAtOffset(schemaDocument, offset);
if (!node?.isSchema) {
return [];
}

const dialectUri = /** @type {string} */ (node.root.dialectUri);
const definitionsKeyword = getKeywordName(dialectUri, "https://json-schema.org/keyword/definitions");

const definitionsNode = SchemaNode.step(definitionsKeyword, node.root);
let highestDefNumber = 0;
if (definitionsNode) {
let defNodeKeys = SchemaNode.keys(definitionsNode);
for (const key of defNodeKeys) {
const keyValue = /** @type {string} */ (SchemaNode.value(key));

const match = /^def(\d+)$/.exec(keyValue);
if (match) {
highestDefNumber = Math.max(parseInt(match[1], 10), highestDefNumber);
}
}
}

const newDefName = `def${highestDefNumber + 1}`;
const extractedDef = schemaDocument.textDocument.getText(range);
const settings = await this.configuration.get();
const lastDefinition = definitionsNode?.children.at(-1);
const lastDefinitionPosition = (lastDefinition?.offset && lastDefinition?.textLength)
? lastDefinition.offset + lastDefinition.textLength
: /** @type {number} */ (definitionsNode?.offset) + 1;
/** @type {CodeAction} */
const codeAction = {
title: `Extract '${newDefName}' to ${definitionsKeyword}`,
kind: CodeActionKind.RefactorExtract,
edit: {
documentChanges: [
TextDocumentEdit.create({ uri: textDocument.uri, version: null }, [
{
range: range,
newText: `{ "$ref": "#/${definitionsKeyword}/${newDefName}" }`
},
definitionsNode
? withFormatting(schemaDocument.textDocument, {
range: {
start: schemaDocument.textDocument.positionAt(lastDefinitionPosition),
end: schemaDocument.textDocument.positionAt(lastDefinitionPosition)
},
newText: lastDefinition ? `,\n"${newDefName}": ${extractedDef}` : `\n"${newDefName}": ${extractedDef}`
}, settings)
: withFormatting(schemaDocument.textDocument, {
range: {
start: schemaDocument.textDocument.positionAt(node.root.offset + node.root.textLength - 2),
end: schemaDocument.textDocument.positionAt(node.root.offset + node.root.textLength - 2)
},
newText: `,\n"${definitionsKeyword}": {\n"${newDefName}": ${extractedDef}\n}`
}, settings)
])
]
}
};

return [codeAction];
});
}
}
4 changes: 1 addition & 3 deletions language-server/src/features/completion/completion.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { TestClient } from "../../test/test-client.ts";

import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Completion", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeAll(async () => {
client = new TestClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { CompletionRequest } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";
import { ifThenPatternCompletion } from "./if-then-completion.js";

import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - if/then completion", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;
let documentUri: string;

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
import { CompletionRequest, CompletionItemKind } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";

import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - if/then completion", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;
let documentUri: string;

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
import { CompletionItemKind, CompletionRequest } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";

import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - $schema completion", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;
let documentUri: string;

beforeAll(async () => {
Expand Down
3 changes: 1 addition & 2 deletions language-server/src/features/diagnostics/deprecated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { DiagnosticSeverity, DiagnosticTag, PublishDiagnosticsNotification } fro
import { TestClient } from "../../test/test-client.ts";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Deprecated", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { PublishDiagnosticsNotification } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Custom Dialects", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;
let documentUriB: string;
let documentUri: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { PublishDiagnosticsNotification } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Validate References Errors", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { DiagnosticSeverity, PublishDiagnosticsNotification } from "vscode-langu
import { TestClient } from "../../test/test-client.ts";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Validate $vocabulary", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { PublishDiagnosticsNotification } from "vscode-languageserver";
import { TestClient } from "../../test/test-client.ts";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "../../services/configuration.js";


describe("Feature - Validation Errors", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
4 changes: 1 addition & 3 deletions language-server/src/features/find-references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { ReferencesRequest } from "vscode-languageserver";
import { TestClient } from "../test/test-client.ts";

import type { DocumentSettings } from "../services/configuration.js";


describe("Feature - References", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
4 changes: 1 addition & 3 deletions language-server/src/features/goto-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { DefinitionRequest } from "vscode-languageserver";
import { TestClient } from "../test/test-client.ts";

import type { DocumentSettings } from "../services/configuration.js";


describe("Feature - Goto Definition", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
Expand Down
3 changes: 1 addition & 2 deletions language-server/src/features/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { HoverRequest, MarkupKind } from "vscode-languageserver";
import { TestClient } from "../test/test-client.ts";

import type { Hover, MarkupContent } from "vscode-languageserver";
import type { DocumentSettings } from "../services/configuration.js";


describe("Feature - Hover", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;

beforeAll(async () => {
client = new TestClient();
Expand Down
4 changes: 1 addition & 3 deletions language-server/src/features/semantic-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { beforeAll, afterAll, afterEach, describe, expect, test } from "vitest";
import { SemanticTokensRequest } from "vscode-languageserver";
import { TestClient } from "../test/test-client.ts";

import type { DocumentSettings } from "../services/configuration.js";


describe("Feature - Semantic Tokens", () => {
let client: TestClient<DocumentSettings>;
let client: TestClient;
let documentUri: string;

beforeAll(async () => {
Expand Down
Loading