How to use the web-tree-sitter.init function in web-tree-sitter

To help you get started, we’ve selected a few web-tree-sitter examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cancerberoSgx / univac / test / probes / treeSitter.ts View on Github external
(async function f() {

  await Parser.init()
  const parser = new Parser()

  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm')
  parser.setLanguage(Lang)

  // let's parse the code of this file itself:
  const tree = parser.parse('export class C { get p() { return 1 } }')
  console.log(dump(tree.rootNode).trim())

})()
github georgewfraser / vscode-tree-sitter / src / extension.ts View on Github external
}
	return vscode.window.createTextEditorDecorationType(options)
}

// Load styles from the current active theme
async function loadStyles() {
	await scopes.load()
	// Clear old styles
	for (const style of decorationCache.values()) {
		style.dispose()
	}
	decorationCache.clear()
}

// For some reason this crashes if we put it inside activate
const initParser = Parser.init() // TODO this isn't a field, suppress package member coloring like Go

// Called when the extension is first activated by user opening a file with the appropriate language
export async function activate(context: vscode.ExtensionContext) {
	console.log("Activating tree-sitter...")
	// Parse of all visible documents
	const trees: {[uri: string]: Parser.Tree} = {}
	async function open(editor: vscode.TextEditor) {
		const language = languages[editor.document.languageId]
		if (language == null) return
		if (language.parser == null) {
			const absolute = path.join(context.extensionPath, 'parsers', language.module + '.wasm')
			const wasm = path.relative(process.cwd(), absolute)
			const lang = await Parser.Language.load(wasm)
			const parser = new Parser()
			parser.setLanguage(lang)
			language.parser = parser
github cancerberoSgx / univac / univac / src / parseAst.ts View on Github external
parseTime = Date.now() - parseT0

    postProcessingT0 = Date.now()
    const visitor = new Visitor()

    visitor.options = options
    tree.accept(visitor)
    ast = visitor.getAst()
  }
  else if (info.treeSitterParser) {
    options.basePath = options.basePath || ''
    let parser: Parser = wasmLoaded[options.basePath] as any

    if (!parser) {
      
      await Parser.init()
  options.debug && console.log('Parser.init()')

      parser = new Parser()
      const Lang = await Parser.Language.load(pathJoin(options.basePath, info.treeSitterParser))
  options.debug && console.log('load')

      parser.setLanguage(Lang)
  options.debug && console.log('setLanguage')

      wasmLoaded[options.basePath] = parser
    }
    // TODO; don't load again the .wasm if already loaded.
    const tree = parser.parse(input)
    parseTime = Date.now() - parseT0
    postProcessingT0 = Date.now()
github EvgeniyPeshkov / syntax-highlighter / src / extension.ts View on Github external
import * as vscode from 'vscode';
import * as parser from 'web-tree-sitter';
import * as jsonc from 'jsonc-parser';
import * as fs from 'fs';
import * as path from 'path';
import { clearTimeout } from 'timers';

// Grammar class
const parserPromise = parser.init();
class Grammar {
    // Parser
    readonly lang: string;
    parser: parser;
    // Grammar
    readonly simpleTerms: { [sym: string]: string } = {};
    readonly complexTerms: string[] = [];
    readonly complexScopes: { [sym: string]: string } = {};
    readonly complexDepth: number;
    readonly complexOrder: boolean;

    constructor(lang: string) {
        // Grammar
        this.lang = lang;
        const grammarFile = __dirname + "/../grammars/" + lang + ".json";
        const grammarJson = jsonc.parse(fs.readFileSync(grammarFile).toString());
github boilerplatter / learn / src / extension.ts View on Github external
lspClient.code2ProtocolConverter.asTextDocumentPositionParams(
            document,
            position.translate(0, -1)
          ),
          token
        );

        lspClient.protocol2CodeConverter.asHover(lspResponse);

        return null;
      }
    });
  }

  // block on parser initialization from WASM
  await Parser.init(); // TODO: double-check that this doesn't crash, as the tree-sitter extension suggests

  // load Rust language module
  const absoluteRustPath = path.join(
    context.extensionPath,
    "parsers",
    `${RUST_WASM_MODULE}.wasm`
  );
  const Rust = await Parser.Language.load(absoluteRustPath); // TODO: cache these parsers between files or init on workspace open
  const parser = new Parser();

  // set Rust as standard tree-sitter language module
  parser.setLanguage(Rust);

  // TODO: this should probably be constrained a bit
  function getTree(document: any) {
    // TODO: investigate getText vs Parser.input()
github elm-tooling / elm-language-server / src / index.ts View on Github external
async (params: InitializeParams): Promise => {
    await Parser.init();
    const absolute = Path.join(__dirname, "tree-sitter-elm.wasm");
    const pathToWasm = Path.relative(process.cwd(), absolute);
    connection.console.info(
      `Loading Elm tree-sitter syntax from ${pathToWasm}`,
    );
    const language = await Parser.Language.load(pathToWasm);
    const parser = new Parser();
    parser.setLanguage(language);

    const { Server } = await import("./server");
    server = new Server(connection, params, parser);
    await server.init();

    return server.capabilities;
  },
);
github mads-hartmann / bash-language-server / server / src / parser.ts View on Github external
export async function initializeParser(): Promise {
  await Parser.init()
  const parser = new Parser()

  /**
   * See https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web#generate-wasm-language-files
   *
   * To compile:
   *  yarn add --dev tree-sitter-cli
   *  npx tree-sitter build-wasm node_modules/tree-sitter-bash
   *
   * The current files was compiled with:
   * "tree-sitter-bash": "^0.16.0",
   * "tree-sitter-cli": "^0.15.9"
   */
  const lang = await Parser.Language.load(`${__dirname}/../tree-sitter-bash.wasm`)

  parser.setLanguage(lang)

web-tree-sitter

Tree-sitter bindings for the web

MIT
Latest version published 11 days ago

Package Health Score

95 / 100
Full package analysis