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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ jobs:

- run: npx hereby generate

- run: node ./internal/lsp/lsproto/_generate/fetchModel.mjs
- run: node ./internal/lsp/lsproto/_generate/generate.mjs
- run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/fetchModel.mts
- run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/generate.mts

- name: Regenerate fourslash tests and update failing test list
run: npm run updatefailing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import which from "which";

/**
* @import { MetaModel, OrType, Type, Request, Notification } from "./metaModelSchema.mts"
*/
void 0;
import type {
MetaModel,
Notification,
OrType,
Request,
Type,
} from "./metaModelSchema.mts";

const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);
Expand All @@ -22,44 +24,30 @@ if (!fs.existsSync(metaModelPath)) {
process.exit(1);
}

/** @type {MetaModel} */
const model = JSON.parse(fs.readFileSync(metaModelPath, "utf-8"));
const model: MetaModel = JSON.parse(fs.readFileSync(metaModelPath, "utf-8"));

/**
* Represents a type in our intermediate type system
* @typedef {Object} GoType
* @property {string} name - Name of the type in Go
* @property {boolean} needsPointer - Whether this type should be used with a pointer
*/
interface GoType {
name: string;
needsPointer: boolean;
}

/**
* @typedef {Object} TypeInfo
* @property {Map<string, GoType>} types - Map of type names to types
* @property {Map<string, string>} literalTypes - Map from literal values to type names
* @property {Map<string, {name: string, type: Type}[]>} unionTypes - Map of union type names to their component types
*/
interface TypeInfo {
types: Map<string, GoType>;
literalTypes: Map<string, string>;
unionTypes: Map<string, { name: string; type: Type; }[]>;
}

/**
* @type {TypeInfo}
*/
const typeInfo = {
const typeInfo: TypeInfo = {
types: new Map(),
literalTypes: new Map(),
unionTypes: new Map(),
};

/**
* @param {string} s
*/
function titleCase(s) {
function titleCase(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

/**
* @param {Type} type
* @returns {GoType}
*/
function resolveType(type) {
function resolveType(type: Type): GoType {
switch (type.kind) {
case "base":
switch (type.name) {
Expand Down Expand Up @@ -161,11 +149,7 @@ function resolveType(type) {
}
}

/**
* @param {OrType} orType
* @returns {GoType}
*/
function handleOrType(orType) {
function handleOrType(orType: OrType): GoType {
const types = orType.items;

// Check for nullable types (OR with null)
Expand Down Expand Up @@ -280,15 +264,10 @@ function collectTypeDefinitions() {
}
}

/**
* @param {string | undefined} s
* @returns {string}
*/
function formatDocumentation(s) {
function formatDocumentation(s: string | undefined): string {
if (!s) return "";

/** @type {string[]} */
let lines = [];
let lines: string[] = [];

for (let line of s.split("\n")) {
line = line.trimEnd();
Expand Down Expand Up @@ -316,36 +295,26 @@ function formatDocumentation(s) {
return lines.length > 0 ? "// " + lines.join("\n// ") + "\n" : "";
}

/**
* @param {string} name
*/
function methodNameIdentifier(name) {
function methodNameIdentifier(name: string) {
return name.split("/").map(v => v === "$" ? "" : titleCase(v)).join("");
}

/**
* Generate the Go code
*/
function generateCode() {
/** @type {string[]} */
const parts = [];
const parts: string[] = [];

/**
* @param {string} s
*/
function write(s) {
function write(s: string) {
parts.push(s);
}

/**
* @param {string} s
*/
function writeLine(s = "") {
parts.push(s + "\n");
}

// File header
writeLine("// Code generated by generate.mjs; DO NOT EDIT.");
writeLine("// Code generated by generate.mts; DO NOT EDIT.");
writeLine("");
writeLine("package lsproto");
writeLine("");
Expand All @@ -361,11 +330,7 @@ function generateCode() {
writeLine("// Structures\n");

for (const structure of model.structures) {
/**
* @param {string} name
* @param {boolean} includeDocumentation
*/
function generateStructFields(name, includeDocumentation) {
function generateStructFields(name: string, includeDocumentation: boolean) {
if (includeDocumentation) {
write(formatDocumentation(structure.documentation));
}
Expand Down Expand Up @@ -533,8 +498,7 @@ function generateCode() {
writeLine("");
}

/** @type {(Request | Notification)[]} */
const requestsAndNotifications = [...model.requests, ...model.notifications];
const requestsAndNotifications: (Request | Notification)[] = [...model.requests, ...model.notifications];

// Generate unmarshalParams function
writeLine("func unmarshalParams(method Method, data []byte) (any, error) {");
Expand Down
2 changes: 2 additions & 0 deletions internal/lsp/lsproto/_generate/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"types": ["node"],
"noUnusedLocals": true,
"noUnusedParameters": true,
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/lsproto/lsp_generated.go

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

Loading