tree-sitter fun 👀

This commit is contained in:
Noah Santschi-Cooney 2022-03-19 22:58:01 +00:00
parent 86100aa008
commit d3c0869288
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
9 changed files with 198 additions and 5 deletions

View file

@ -1,3 +1,4 @@
import path = require('path')
import * as vscode from 'vscode'
import * as lsp from 'vscode-languageclient'
import { Extension } from './extension'
@ -30,7 +31,7 @@ export function virtualMergedDocument(e: Extension): Command {
command: 'virtualMerge',
arguments: [path]
})
} catch(e) {}
} catch (e) { }
return content
}
@ -40,17 +41,67 @@ export function virtualMergedDocument(e: Extension): Command {
onDidChange = this.onDidChangeEmitter.event
provideTextDocumentContent(uri: vscode.Uri, __: vscode.CancellationToken): vscode.ProviderResult<string> {
return getVirtualDocument(uri.path)
return getVirtualDocument(uri.path.replace('.flattened' + path.extname(uri.path), path.extname(uri.path)))
}
}
e.context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('mcglsl', docProvider))
return async () => {
const uri = vscode.window.activeTextEditor.document.uri
const path = vscode.Uri.parse('mcglsl:' + uri.path)
if (vscode.window.activeTextEditor.document.languageId != 'glsl') return
const uri = vscode.window.activeTextEditor.document.uri.path
.substring(0, vscode.window.activeTextEditor.document.uri.path.lastIndexOf('.'))
+ '.flattened.'
+ vscode.window.activeTextEditor.document.uri.path
.slice(vscode.window.activeTextEditor.document.uri.path.lastIndexOf('.') + 1)
const path = vscode.Uri.parse(`mcglsl:${uri}`)
const doc = await vscode.workspace.openTextDocument(path)
docProvider.onDidChangeEmitter.fire(path)
await vscode.window.showTextDocument(doc, {preview: true})
await vscode.window.showTextDocument(doc, {
viewColumn: vscode.ViewColumn.Two,
preview: true
})
}
}
export function parseTree(e: Extension): Command {
const getVirtualDocument = async (path: string): Promise<string | null> => {
let content: string = ''
try {
content = await e.lspClient.sendRequest<string>(lsp.ExecuteCommandRequest.type.method, {
command: 'parseTree',
arguments: [path]
})
} catch (e) { }
return content
}
const docProvider = new class implements vscode.TextDocumentContentProvider {
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>()
onDidChange = this.onDidChangeEmitter.event
provideTextDocumentContent(uri: vscode.Uri, __: vscode.CancellationToken): vscode.ProviderResult<string> {
if (uri.path.includes('.flattened.')) return ''
return getVirtualDocument(uri.path.substring(0, uri.path.lastIndexOf('.')))
}
}
e.context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('mcglsl', docProvider))
return async () => {
if (vscode.window.activeTextEditor.document.languageId != 'glsl') return
const uri = vscode.window.activeTextEditor.document.uri
const path = vscode.Uri.parse(`mcglsl:${uri.path}.ast`)
const doc = await vscode.workspace.openTextDocument(path)
docProvider.onDidChangeEmitter.fire(path)
await vscode.window.showTextDocument(doc, {
viewColumn: vscode.ViewColumn.Two,
preview: true
})
}
}

View file

@ -48,6 +48,7 @@ export class Extension {
this.registerCommand('graphDot', commands.generateGraphDot)
this.registerCommand('restart', commands.restartExtension)
this.registerCommand('virtualMerge', commands.virtualMergedDocument)
this.registerCommand('parseTree', commands.parseTree)
log.info('starting language server...')