-
Notifications
You must be signed in to change notification settings - Fork 85
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0cea9af
feat: metadata autocompletion
Nemikolh 1d4cfa1
feat: provide a description for every property.
Nemikolh e29943d
Merge branch 'main' into joan/frontmatter-completions
Nemikolh 2b481dd
fix: issues with schema re-generation
Nemikolh 07d15fc
Merge branch 'main' into joan/frontmatter-completions
Nemikolh 143bde7
fix: update tsconfig
Nemikolh 4048d86
Merge remote-tracking branch 'upstream/main' into joan/frontmatter-co…
AriPerkkio e4a5f0e
fix: code review
AriPerkkio 81bed35
Merge branch 'main' into joan/frontmatter-completions
AriPerkkio a085608
Merge branch 'main' into joan/frontmatter-completions
d3lm 1e6b67f
fix: code review
AriPerkkio 0966dcf
Merge branch 'main' into joan/frontmatter-completions
AriPerkkio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,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'); | ||
} | ||
|
||
/** | ||
* @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); | ||
}; | ||
} |
This file contains hidden or 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,5 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
import { zodToJsonSchema } from 'zod-to-json-schema'; | ||
import { contentSchema } from '@tutorialkit/types'; | ||
|
||
parentPort.postMessage(zodToJsonSchema(contentSchema)); |
This file contains hidden or 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 |
---|---|---|
@@ -1,16 +1,52 @@ | ||
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(); | ||
} |
This file contains hidden or 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,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', | ||
schema, | ||
fileMatch: [ | ||
'**/*', | ||
|
||
// TODO: these don't work | ||
'src/content/*.md', | ||
'src/content/**/*.md', | ||
'src/content/**/*.mdx', | ||
], | ||
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.