-
Notifications
You must be signed in to change notification settings - Fork 88
feat(extension): metadata autocompletion #143
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
Changes from 5 commits
0cea9af
1d4cfa1
e29943d
2b481dd
07d15fc
143bde7
4048d86
e4a5f0e
81bed35
a085608
1e6b67f
0966dcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { watch } from 'chokidar'; | ||
import * as esbuild from 'esbuild'; | ||
import { execa } from 'execa'; | ||
import fs from 'node:fs'; | ||
import { createRequire } from 'node:module'; | ||
import { join, dirname } from 'path'; | ||
import { Worker } from 'node:worker_threads'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const require = createRequire(import.meta.url); | ||
const production = process.argv.includes('--production'); | ||
const isWatch = process.argv.includes('--watch'); | ||
|
||
async function main() { | ||
const ctx = await esbuild.context({ | ||
entryPoints: { | ||
extension: 'src/extension.ts', | ||
server: './src/language-server/index.ts', | ||
}, | ||
bundle: true, | ||
format: 'cjs', | ||
minify: production, | ||
sourcemap: !production, | ||
sourcesContent: false, | ||
tsconfig: './tsconfig.json', | ||
platform: 'node', | ||
outdir: 'dist', | ||
define: { 'process.env.NODE_ENV': production ? '"production"' : '"development"' }, | ||
external: ['vscode'], | ||
plugins: [esbuildUMD2ESMPlugin], | ||
}); | ||
|
||
if (isWatch) { | ||
const buildMetadataSchemaDebounced = debounce(buildMetadataSchema, 100); | ||
const dependencyPath = dirname(require.resolve('@tutorialkit/types')); | ||
|
||
watch(dependencyPath).on('all', (eventName, path) => { | ||
if (eventName !== 'change' && eventName !== 'add' && eventName !== 'unlink') { | ||
return; | ||
} | ||
|
||
buildMetadataSchemaDebounced(); | ||
}); | ||
|
||
await Promise.all([ | ||
ctx.watch(), | ||
execa('tsc', ['--noEmit', '--watch', '--preserveWatchOutput', '--project', 'tsconfig.json'], { | ||
stdio: 'inherit', | ||
preferLocal: true, | ||
}), | ||
]); | ||
} else { | ||
await ctx.rebuild(); | ||
await ctx.dispose(); | ||
|
||
await buildMetadataSchema(); | ||
|
||
if (production) { | ||
// rename name in package json to match extension name on store: | ||
const pkgJSON = JSON.parse(fs.readFileSync('./package.json', { encoding: 'utf8' })); | ||
|
||
pkgJSON.name = 'tutorialkit'; | ||
|
||
fs.writeFileSync('./package.json', JSON.stringify(pkgJSON, undefined, 2), 'utf8'); | ||
} | ||
} | ||
} | ||
|
||
async function buildMetadataSchema() { | ||
const schema = await new Promise((resolve) => { | ||
const worker = new Worker(join(__dirname, './load-schema-worker.mjs')); | ||
worker.on('message', (value) => resolve(value)); | ||
}); | ||
|
||
fs.mkdirSync('./dist', { recursive: true }); | ||
fs.writeFileSync('./dist/schema.json', JSON.stringify(schema, undefined, 2), 'utf-8'); | ||
|
||
console.log('Updated schema.json'); | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @type {import('esbuild').Plugin} | ||
*/ | ||
const esbuildUMD2ESMPlugin = { | ||
name: 'umd2esm', | ||
setup(build) { | ||
build.onResolve({ filter: /^(vscode-.*-languageservice|jsonc-parser)/ }, (args) => { | ||
const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] }); | ||
const pathEsm = pathUmdMay.replace('/umd/', '/esm/').replace('\\umd\\', '\\esm\\'); | ||
|
||
return { path: pathEsm }; | ||
}); | ||
}, | ||
}; | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
/** | ||
* Debounce the provided function. | ||
* | ||
* @param {Function} fn Function to debounce | ||
* @param {number} duration Duration of the debounce | ||
* @returns {Function} Debounced function | ||
*/ | ||
function debounce(fn, duration) { | ||
let timeoutId = 0; | ||
|
||
return function () { | ||
clearTimeout(timeoutId); | ||
|
||
timeoutId = setTimeout(fn.bind(this), duration, ...arguments); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
import { zodToJsonSchema } from 'zod-to-json-schema'; | ||
import { chapterSchema, lessonSchema, partSchema, tutorialSchema } from '@tutorialkit/types'; | ||
|
||
const schema = tutorialSchema.strict().or(partSchema.strict()).or(chapterSchema.strict()).or(lessonSchema.strict()); | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
parentPort.postMessage(zodToJsonSchema(schema)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
import * as serverProtocol from '@volar/language-server/protocol'; | ||
import { createLabsInfo } from '@volar/vscode'; | ||
import * as vscode from 'vscode'; | ||
import { useCommands } from './commands'; | ||
import { useLessonTree } from './views/lessonsTree'; | ||
import * as lsp from 'vscode-languageclient/node'; | ||
|
||
export let extContext: vscode.ExtensionContext; | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
let client: lsp.BaseLanguageClient; | ||
|
||
export async function activate(context: vscode.ExtensionContext) { | ||
extContext = context; | ||
|
||
useCommands(); | ||
useLessonTree(); | ||
|
||
const serverModule = vscode.Uri.joinPath(context.extensionUri, 'dist', 'server.js'); | ||
const runOptions = { execArgv: <string[]>[] }; | ||
const debugOptions = { execArgv: ['--nolazy', '--inspect=' + 6009] }; | ||
const serverOptions: lsp.ServerOptions = { | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
run: { | ||
module: serverModule.fsPath, | ||
transport: lsp.TransportKind.ipc, | ||
options: runOptions, | ||
}, | ||
debug: { | ||
module: serverModule.fsPath, | ||
transport: lsp.TransportKind.ipc, | ||
options: debugOptions, | ||
}, | ||
}; | ||
const clientOptions: lsp.LanguageClientOptions = { | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
documentSelector: [{ language: 'markdown' }, { language: 'mdx' }], | ||
initializationOptions: {}, | ||
}; | ||
client = new lsp.LanguageClient('tutorialkit-language-server', 'TutorialKit', serverOptions, clientOptions); | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await client.start(); | ||
|
||
const labsInfo = createLabsInfo(serverProtocol); | ||
labsInfo.addLanguageClient(client); | ||
|
||
return labsInfo.extensionExports; | ||
} | ||
|
||
export function deactivate() { | ||
// do nothing | ||
return client?.stop(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createConnection, createServer, createSimpleProject } from '@volar/language-server/node'; | ||
import { create as createYamlService } from 'volar-service-yaml'; | ||
import { SchemaPriority } from 'yaml-language-server'; | ||
import { frontmatterPlugin } from './languagePlugin'; | ||
import { readSchema } from './schema'; | ||
|
||
const connection = createConnection(); | ||
const server = createServer(connection); | ||
|
||
connection.listen(); | ||
|
||
connection.onInitialize((params) => { | ||
const yamlService = createYamlService({ | ||
getLanguageSettings(_context) { | ||
const schema = readSchema(); | ||
|
||
return { | ||
completion: true, | ||
validate: true, | ||
hover: true, | ||
format: true, | ||
yamlVersion: '1.2', | ||
isKubernetes: false, | ||
schemas: [ | ||
{ | ||
uri: 'https://tutorialkit.dev/reference/configuration/', | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
schema, | ||
fileMatch: [ | ||
'**/*', | ||
|
||
// TODO: those don't work | ||
AriPerkkio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'src/content/*.md', | ||
'src/content/**/*.md', | ||
'src/content/**/*.mdx', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still need to investigate that, ideally I don't want the check to apply to every markdown & mdx files. |
||
], | ||
priority: SchemaPriority.Settings, | ||
}, | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
delete yamlService.capabilities.codeLensProvider; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed that because otherwise the schema was popping up on each file metadata. It wasn't a really great UX. |
||
|
||
return server.initialize( | ||
params, | ||
createSimpleProject([frontmatterPlugin(connection.console.debug.bind(connection.console.debug))]), | ||
[yamlService], | ||
); | ||
}); | ||
|
||
connection.onInitialized(server.initialized); | ||
|
||
connection.onShutdown(server.shutdown); |
Uh oh!
There was an error while loading. Please reload this page.