Skip to content
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

Add typechecking for TypeScript #946

Merged
merged 24 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba1c002
feat: use the typescript-language-server for TS
vixalien Jun 18, 2024
892a4ce
feat: add the gi-types submodule
vixalien Jun 18, 2024
cb3defd
feat: use the gi-types for GTK types
vixalien Jun 18, 2024
3c7bf25
feat: add lspc to typescript codeview
vixalien Jun 18, 2024
0f2dbe7
chore: dont save/restore state with the Typescript LSP
vixalien Jun 18, 2024
7555fd0
chore: remove the gi-types path from tsconfig
vixalien Jun 18, 2024
dd2c241
fix: the LSPClient using a one-based position.character
vixalien Jun 18, 2024
ebd62de
chore: return name of copied files in `copy`/`copyDirectory`
vixalien Jun 20, 2024
985742e
feat: notify the LSP after setting up typescript project
vixalien Jun 20, 2024
cde5bfa
chore: add commented types for the global workbench object
vixalien Jul 2, 2024
2df4d72
chore: move `gi-types` submodules from template folder
vixalien Jul 3, 2024
e3ac8f5
feat: dont copy over all types
vixalien Jul 3, 2024
ac6078e
chore: make notify tsconfig message clearer
vixalien Jul 3, 2024
81a94e3
chore: return `Gio.FIle` from `copy`
vixalien Jul 3, 2024
f8a4f69
chore: use single meson.build for typescript
vixalien Jul 3, 2024
080c898
chore: fix unwanted change
vixalien Jul 3, 2024
9a9ac53
chore: add TODO when for fixing ambient.d.ts
vixalien Jul 3, 2024
8868eff
chore: use correct notify for tsconfig change
vixalien Jul 3, 2024
75531a0
chore: revery util.js changes
vixalien Jul 3, 2024
a06b0ec
chore: remove unnecessary change
vixalien Jul 3, 2024
6aad42a
Merge branch 'main' into wip/vixalien/typechecking
sonnyp Jul 3, 2024
b3bdeb9
Merge branch 'main' into wip/vixalien/typechecking
sonnyp Jul 6, 2024
e3553d6
Use pkgdatadir in tsconfig.json
sonnyp Jul 6, 2024
3c4dc9d
fix make ci
sonnyp Jul 6, 2024
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: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
[submodule "blueprint-compiler"]
path = blueprint-compiler
url = https://gitlab.gnome.org/jwestman/blueprint-compiler.git
[submodule "src/langs/typescript/template/gi-types"]
path = src/langs/typescript/gi-types
url = https://gitlab.gnome.org/BrainBlasted/gi-typescript-definitions.git
branch = nightly
5 changes: 4 additions & 1 deletion src/PanelCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function PanelCode({
builder,
previewer,
session: { settings, file },
langs,
}) {
const panel_code = builder.get_object("panel_code");
const button_code = builder.get_object("button_code");
Expand Down Expand Up @@ -59,7 +60,9 @@ export default function PanelCode({
}

if (panel.language.toLowerCase() === "typescript") {
setupTypeScriptProject(file).catch(console.error);
setupTypeScriptProject(file, langs.typescript.document).catch(
console.error,
);
}
}
switchLanguage();
Expand Down
9 changes: 1 addition & 8 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ export const languages = [
document: null,
default_file: "main.ts",
index: 4,
language_server: [
"biome",
"lsp-proxy",
// src/meson.build installs biome.json there
GLib.getenv("FLATPAK_ID")
? `--config-path=${pkg.pkgdatadir}`
: `--config-path=src/langs/typescript`,
],
language_server: ["typescript-language-server", "--stdio"],
formatting_options: {
...formatting_options,
tabSize: 2,
Expand Down
6 changes: 1 addition & 5 deletions src/langs/typescript/TypeScriptDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class TypeScriptDocument extends Document {
super(...args);

this.lspc = setup({ document: this });
this.code_view.lspc = this.lspc;
}

vixalien marked this conversation as resolved.
Show resolved Hide resolved
async format() {
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
const text_edits = await this.lspc.request("textDocument/formatting", {
Expand All @@ -25,10 +25,6 @@ export class TypeScriptDocument extends Document {
},
});

// Biome doesn't support diff - it just returns one edit
// we don't want to loose the cursor position so we use this
const state = this.code_view.saveState();
applyTextEdits(text_edits, this.buffer);
await this.code_view.restoreState(state);
vixalien marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions src/langs/typescript/gi-types
Submodule gi-types added at 396fe1
8 changes: 8 additions & 0 deletions src/langs/typescript/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
install_data(['types/ambient.d.ts'],
install_dir : join_paths(pkgdatadir, 'langs/typescript'),
preserve_path: true)

install_subdir('gi-types',
install_dir : join_paths(pkgdatadir, 'langs/typescript'))

subdir('template')
2 changes: 1 addition & 1 deletion src/langs/typescript/template/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
install_data(['types/ambient.d.ts', 'types/gi-module.d.ts', 'tsconfig.json'],
install_data(['tsconfig.json'],
install_dir : join_paths(pkgdatadir, 'langs/typescript/template'),
vixalien marked this conversation as resolved.
Show resolved Hide resolved
preserve_path: true)
15 changes: 12 additions & 3 deletions src/langs/typescript/template/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
// currently supported by the latest GJS
"target": "ESNext",
"outDir": "compiled_javascript",
"baseUrl": ".",
"paths": {
"gi://*": ["./types/gi-module.d.ts"]
}
"*": [
"*",
"/app/share/re.sonny.Workbench.Devel/langs/typescript/gi-types/*"
]
},
"skipLibCheck": true
},
"include": ["main.ts", "types/ambient.d.ts", "types/gi-module.d.ts"]
"include": [
"main.ts",
"/app/share/re.sonny.Workbench.Devel/langs/typescript/types/ambient.d.ts",
"/app/share/re.sonny.Workbench.Devel/langs/typescript/gi-types/gi.d.ts"
]
}
6 changes: 0 additions & 6 deletions src/langs/typescript/template/types/gi-module.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// import Adw from "gi://Adw";
// import Gtk from "gi://Gtk?version=4.0";
// import GObject from "gi://GObject";

// additional type declarations for GJS

// additional GJS log utils
Expand Down Expand Up @@ -31,6 +35,16 @@ declare module "gettext" {
): string;
}

// TODO: uncomment correct typings
vixalien marked this conversation as resolved.
Show resolved Hide resolved
// declare const workbench: {
// window: Adw.ApplicationWindow;
// application: Adw.Application;
// builder: Gtk.Builder;
// template: string;
// resolve(path: string): string;
// preview(object: Gtk.Widget): void;
// build(params: Record<string, Function | GObject.Object>): void;
// };

// global workbench object
// TODO: use correct typings
declare const workbench: any;
39 changes: 12 additions & 27 deletions src/langs/typescript/typescript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Gio from "gi://Gio";

import { createLSPClient } from "../../common.js";
import { getLanguage, copy } from "../../util.js";
import { copy, getLanguage } from "../../util.js";
vixalien marked this conversation as resolved.
Show resolved Hide resolved
import { isTypeScriptEnabled } from "../../Extensions/Extensions.js";

export function setup({ document }) {
Expand Down Expand Up @@ -40,31 +40,16 @@ const typescript_template_dir = Gio.File.new_for_path(
pkg.pkgdatadir,
).resolve_relative_path("langs/typescript/template");

export async function setupTypeScriptProject(destination) {
const types_destination = destination.get_child("types");

if (!types_destination.query_exists(null)) {
types_destination.make_directory_with_parents(null);
}
export async function setupTypeScriptProject(destination, document) {
const destination_uri = await copy(
"tsconfig.json",
typescript_template_dir,
destination,
Gio.FileCopyFlags.NONE,
);

return Promise.all([
copy(
"types/ambient.d.ts",
typescript_template_dir,
types_destination,
Gio.FileCopyFlags.NONE,
),
copy(
"types/gi-module.d.ts",
typescript_template_dir,
types_destination,
Gio.FileCopyFlags.NONE,
),
copy(
"tsconfig.json",
typescript_template_dir,
destination,
Gio.FileCopyFlags.NONE,
),
]);
// notify the language server that are the tsconfig file was created
vixalien marked this conversation as resolved.
Show resolved Hide resolved
document.lspc._notify("workspace/didCreateFile", {
files: [{ uri: destination_uri }],
});
vixalien marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/lsp/LSPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default class LSPClient {
},
position: {
line: iter_cursor.get_line(),
character: iter_cursor.get_line_offset() - 1,
character: iter_cursor.get_line_offset(),
vixalien marked this conversation as resolved.
Show resolved Hide resolved
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ blueprint_compiler = find_program(
meson.add_install_script('../build-aux/library.js', pkgdatadir)

subdir('langs/rust/template')
subdir('langs/typescript/template')
subdir('langs/typescript')

configure_file(
input: 'bin.js',
Expand Down
13 changes: 11 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ export async function copyDirectory(source, destination) {
null,
);

const children = [];
vixalien marked this conversation as resolved.
Show resolved Hide resolved

for await (const file_info of enumerator) {
const child = enumerator.get_child(file_info);
const child_dest = destination.get_child(child.get_basename());

if (file_info.get_file_type() === Gio.FileType.DIRECTORY) {
await copyDirectory(child, child_dest);
const deep_children = await copyDirectory(child, child_dest);
children.push(...deep_children);
continue;
}

try {
children.push(child_dest.get_uri());
await child.copy_async(
child_dest, // destination
Gio.FileCopyFlags.OVERWRITE, // flags
Expand All @@ -143,6 +147,8 @@ export async function copyDirectory(source, destination) {
}
}
}

return children;
}

export function getNowForFilename() {
Expand Down Expand Up @@ -191,10 +197,11 @@ export function removeDirectory(file) {

export async function copy(filename, source_dir, dest_dir, flags) {
const file = source_dir.get_child(filename);
const dest_file = dest_dir.get_child(file.get_basename());

try {
await file.copy_async(
dest_dir.get_child(file.get_basename()), // destination
dest_file, // destination
flags, // flags
GLib.PRIORITY_DEFAULT, // priority
null, // cancellable
Expand All @@ -205,4 +212,6 @@ export async function copy(filename, source_dir, dest_dir, flags) {
throw err;
}
}

return dest_file.get_uri();
vixalien marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export default function Window({ application, session }) {
builder,
previewer,
session,
langs,
});

previewer.setPanelCode(panel_code);
Expand Down
Loading