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 all 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ unit:
test: unit lint
./build-aux/fun workbench-cli ci demos/src/Welcome

ci: setup test
ci: setup build test
./build-aux/fun workbench-cli ci demos/src/*

# Note that if you have Sdk extensions installed they will be used
Expand Down
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
5 changes: 1 addition & 4 deletions src/langs/typescript/TypeScriptDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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() {
Expand All @@ -25,10 +26,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
}
}
19 changes: 0 additions & 19 deletions src/langs/typescript/biome.json

This file was deleted.

1 change: 1 addition & 0 deletions src/langs/typescript/gi-types
Submodule gi-types added at 396fe1
14 changes: 14 additions & 0 deletions src/langs/typescript/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
configure_file(
input: 'template/tsconfig.json',
output: 'tsconfig.json',
install_dir: join_paths(pkgdatadir, 'langs/typescript/template/'),
configuration: bin_conf,
)

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'))
3 changes: 0 additions & 3 deletions src/langs/typescript/template/meson.build

This file was deleted.

12 changes: 9 additions & 3 deletions src/langs/typescript/template/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
// currently supported by the latest GJS
"target": "ESNext",
"outDir": "compiled_javascript",
"baseUrl": ".",
"paths": {
"gi://*": ["./types/gi-module.d.ts"]
}
"*": ["*", "@pkgdatadir@/langs/typescript/gi-types/*"]
},
"skipLibCheck": true
},
"include": ["main.ts", "types/ambient.d.ts", "types/gi-module.d.ts"]
"include": [
"main.ts",
"@pkgdatadir@/langs/typescript/types/ambient.d.ts",
"@pkgdatadir@/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 after we switch to `ts-for-gir`
// 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;
38 changes: 12 additions & 26 deletions src/langs/typescript/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,17 @@ 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_file = 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 the tsconfig file was created
// to initialized diagnostics and type checkings
await document.lspc.notify("workspace/didCreateFile", {
files: [{ uri: destination_file.get_uri() }],
});
}
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
5 changes: 4 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,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 +206,6 @@ export async function copy(filename, source_dir, dest_dir, flags) {
throw err;
}
}

return dest_file;
}
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