-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commit of the following: commit 705f025 Merge: 915fe99 17e5377 Author: Sonny Piers <[email protected]> Date: Thu May 30 17:35:46 2024 +0300 Merge branch 'main' into typescript commit 915fe99 Author: Angelo Verlain Shema <[email protected]> Date: Tue May 28 11:19:04 2024 +0200 Compile TypeScript to JavaScript before executing (#941) This basically compiles from TypeScript to JavaScript at runtime when the "Run" button is clicked. However, there are currently 2 issues worth mentioning: ### 1. Speed Notice that this is noticeably slow because it's using `tsc`. It could possibly be improved by using `esbuild`, `swc`, `babel` or something similar but then there will be no typechecking when the "Run" button is clicked. However, I think the above typechecking caveat will not make much sense when we have real-time Intellisense in the editor for TypeScript. ### 2. Sourcemaps Another consideration is the lack of sourcemap support in GJS. While tsc can generate sourcemaps, this feature is disabled because GJS won't use them. This means that some errors will have the wrong line:column information. For example: ![image](https://github.com/workbenchdev/Workbench/assets/37999241/c6487292-18a9-4e50-85a0-5c8771f107fc) Workbench/GJS reports the error is on line number 17 even if it's actually on line number 22 because that's where it ends up after it's compiled to JavaScript (many compilers will eat up unnecessary line breaks even though the minify option is turned off). I left some TODOs in here where some decisions need to be made and hope to get some feedback commit 773669f Author: Angelo Verlain Shema <[email protected]> Date: Tue May 7 15:53:06 2024 +0200 Add base TypeScript view to Workbench (#938)
- Loading branch information
Showing
23 changed files
with
412 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Gio from "gi://Gio"; | ||
import GLib from "gi://GLib"; | ||
|
||
import { buildRuntimePath } from "../../util.js"; | ||
|
||
export default function JavascriptBuilder() { | ||
async function run(text) { | ||
// We have to create a new file each time | ||
// because gjs doesn't appear to use etag for module caching | ||
// ?foo=Date.now() also does not work as expected | ||
// https://gitlab.gnome.org/GNOME/gjs/-/issues/618 | ||
const path = buildRuntimePath(`workbench-${Date.now()}`); | ||
const file_javascript = Gio.File.new_for_path(path); | ||
await file_javascript.replace_contents_async( | ||
new GLib.Bytes(text), | ||
null, | ||
false, | ||
Gio.FileCreateFlags.NONE, | ||
null, | ||
); | ||
|
||
let exports; | ||
try { | ||
exports = await import(`file://${file_javascript.get_path()}`); | ||
} catch (err) { | ||
console.error(err); | ||
return false; | ||
} finally { | ||
file_javascript | ||
.delete_async(GLib.PRIORITY_DEFAULT, null) | ||
.catch(console.error); | ||
} | ||
|
||
return exports; | ||
} | ||
|
||
return { run }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Gio from "gi://Gio"; | ||
import GLib from "gi://GLib"; | ||
|
||
import { buildRuntimePath, copy } from "../../util.js"; | ||
|
||
export default function Compiler({ session }) { | ||
const { file } = session; | ||
|
||
async function compile() { | ||
const tsc_launcher = new Gio.SubprocessLauncher(); | ||
tsc_launcher.set_cwd(file.get_path()); | ||
|
||
const tsc = tsc_launcher.spawnv(["tsc", "--project", file.get_path()]); | ||
await tsc.wait_async(null); | ||
|
||
const result = tsc.get_successful(); | ||
tsc_launcher.close(); | ||
return result; | ||
} | ||
|
||
async function run() { | ||
// We have to create a new file each time | ||
// because gjs doesn't appear to use etag for module caching | ||
// ?foo=Date.now() also does not work as expected | ||
// https://gitlab.gnome.org/GNOME/gjs/-/issues/618 | ||
const path = buildRuntimePath(`workbench-${Date.now()}`); | ||
const compiled_dir = Gio.File.new_for_path(path); | ||
if (!compiled_dir.query_exists(null)) { | ||
await compiled_dir.make_directory_async(GLib.PRIORITY_DEFAULT, null); | ||
} | ||
await copy( | ||
"main.js", | ||
file.get_child("compiled_javascript"), | ||
compiled_dir, | ||
Gio.FileCopyFlags.NONE, | ||
); | ||
const compiled_file = compiled_dir.get_child("main.js"); | ||
|
||
let exports; | ||
try { | ||
exports = await import(`file://${compiled_file.get_path()}`); | ||
} catch (err) { | ||
console.error(err); | ||
return false; | ||
} finally { | ||
compiled_file | ||
.delete_async(GLib.PRIORITY_DEFAULT, null) | ||
.catch(console.error); | ||
} | ||
|
||
return exports; | ||
} | ||
|
||
return { compile, run }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { setup } from "./typescript.js"; | ||
|
||
import Document from "../../Document.js"; | ||
import { applyTextEdits } from "../../lsp/sourceview.js"; | ||
|
||
export class TypeScriptDocument extends Document { | ||
constructor(...args) { | ||
super(...args); | ||
|
||
this.lspc = setup({ document: this }); | ||
} | ||
|
||
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", { | ||
textDocument: { | ||
uri: this.file.get_uri(), | ||
}, | ||
options: { | ||
tabSize: 2, | ||
insertSpaces: true, | ||
trimTrailingWhitespace: true, | ||
insertFinalNewline: true, | ||
trimFinalNewlines: true, | ||
}, | ||
}); | ||
|
||
// 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.3.3/schema.json", | ||
"javascript": { | ||
"globals": ["workbench"] | ||
}, | ||
"formatter": { | ||
"indentStyle": "space", | ||
"indentWidth": 2 | ||
}, | ||
"linter": { | ||
"rules": { | ||
"recommended": false, | ||
"correctness": { | ||
"noUndeclaredVariables": "error", | ||
"noUnusedVariables": "warn" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
install_data(['types/ambient.d.ts', 'types/gi-module.d.ts', 'tsconfig.json'], | ||
install_dir : join_paths(pkgdatadir, 'langs/typescript/template'), | ||
preserve_path: true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
// TODO: should probably be fixed to ES2023, or whatever standard is | ||
// currently supported by the latest GJS | ||
"target": "ESNext", | ||
"outDir": "compiled_javascript", | ||
"paths": { | ||
"gi://*": ["./types/gi-module.d.ts"] | ||
} | ||
}, | ||
"include": ["main.ts", "types/ambient.d.ts", "types/gi-module.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// additional type declarations for GJS | ||
|
||
// additional GJS log utils | ||
declare function print(...args: any[]): void; | ||
declare function log(...args: any[]): void; | ||
|
||
// GJS pkg global | ||
declare const pkg: { | ||
version: string; | ||
name: string; | ||
}; | ||
|
||
// old GJS global imports | ||
// used like: imports.format.printf("..."); | ||
declare module imports { | ||
// format import | ||
const format: { | ||
format(this: String, ...args: any[]): string; | ||
printf(fmt: string, ...args: any[]): string; | ||
vprintf(fmt: string, args: any[]): string; | ||
}; | ||
} | ||
|
||
// gettext import | ||
declare module "gettext" { | ||
export function gettext(id: string): string; | ||
export function ngettext( | ||
singular: string, | ||
plural: string, | ||
n: number, | ||
): string; | ||
} | ||
|
||
// global workbench object | ||
// TODO: use correct typings | ||
declare const workbench: any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// dummy exports for a module imported with "gi://*" | ||
// will be replaced later with actual gi-types | ||
|
||
declare const module: any; | ||
|
||
export default module; |
Oops, something went wrong.