Skip to content

Instantly share code, notes, and snippets.

@leodevbro
Created March 16, 2022 11:32
Show Gist options
  • Save leodevbro/a8e9b9f4a6c8545d9ca217bb77704606 to your computer and use it in GitHub Desktop.
Save leodevbro/a8e9b9f4a6c8545d9ca217bb77704606 to your computer and use it in GitHub Desktop.
/* eslint-disable @typescript-eslint/naming-convention */
// import * as path from "path";
// import * as vscode from "vscode";
import { IExtensionPackage, IGrammar } from "./IExtensionGrammar";
// import fs = require("fs");
import { getRegexForBrackets } from "./bracketUtil";
// import JSON5 = require("json5");
import * as JSON5 from "json5";
import LanguageConfig from "./languageConfig";
// var ab2str = require("arraybuffer-to-string");
import stripJsonComments from "strip-json-comments";
// var jsonminify = require("jsonminify");
import stripJsonTrailingCommas from 'strip-json-trailing-commas';
import * as vscode from "vscode";
import * as path from "path";
import * as nodeFs from "fs";
import * as bToBuf from "base64-arraybuffer";
import { jjj7 } from "./kkkk";
// @ts-ignore
import gggP from "./onig.wasm";
const wasmPPP = path.resolve(__dirname, "onig.wasm");
// console.log("wasmPPP");
// console.log(wasmPPP);
// console.log("dirrrrr:", __dirname, __filename);
import myOniguruma = require("vscode-oniguruma");
const myOnigurumaPath = require.resolve("vscode-oniguruma");
import myTextmate = require("vscode-textmate");
import { IRawGrammar } from "vscode-textmate/release/types";
import { resolve } from "path";
// const myOnigurumaPath = "./node_modules/vscode-oniguruma/release/onig.wasm";
// const ggg = myTextmate();
const parseRawGrammar000 = myTextmate.parseRawGrammar;
// const bbbfff = new ArrayBuffer(jjj7);
const bbbfff = bToBuf.decode(jjj7); // +++++++++++++++++++++++
// console.log("bbbfff");
// console.log(bbbfff);
// console.log("fff888");
// console.log(fff888);
// console.log("vscode-oniguruma path:");
// console.log(myOnigurumaPath);
// const ppp = path.join(
// vscode.env.appRoot,
// "node_modules.asar",
// "vscode-oniguruma",
// );
// const hhh = vscode.env.appRoot;
// const myOnigurumaPath2 = vscode.Uri.file();
// console.log("myOnigurumaPath:", myOnigurumaPath, typeof myOnigurumaPath, path);
// console.log("rooooo:");
// console.log(hhh);
// console.log(typeof hhh);
// @ts-ignore
// import * as hhh from "node_modules-path";
// console.log("rbbbbb333:");
// console.log(hhh);
// console.log(typeof hhh);
// const myWasmPath = path.join(
// myOnigurumaPath.slice(0, myOnigurumaPath.length - 7),
// "onig.wasm",
// );
// const myWasmPathUri = vscode.Uri.file(myWasmPath);
// const myWasmPathUri = vscode.Uri.file(`\\onig.wasm`);
const myWasmPathUri = vscode.Uri.file(wasmPPP);
// console.log("myWasmPathUri:");
// console.log(myWasmPathUri);
myOniguruma.loadWASM(bbbfff) // ??????????
.then((x) => {
console.log("successful"); //aazz
})
.catch((error) => {
console.log("error: ", error); //aazz
});
// const exHhh = vscode.extensions.getExtension("vscode.coffeescript");
// export const exHhh2 = vscode.extensions.all;
// console.log("kFFFFFFFFFFFFFFFFF", exHhh, exHhh2.length); //aazz
let ob = {
t: null,
} as {
t: NodeJS.Timeout | null;
};
if (ob.t) {
clearTimeout(ob.t);
}
// ob.t = setTimeout(() => {
// const theAll = vscode.extensions.all;
// console.log("theAll:");
// console.log(theAll.length);
// console.log("theAllend");
// }, 10000);
/*
vscode.workspace.fs.readFile(myWasmPathUri).then((myUint8Array) => {
const vscodeFsBuffer = myUint8Array.buffer;
// console.log("vscodeFsBuffer: ", vscodeFsBuffer);
// const nodeFsBuffer = nodeFs.readFileSync(myWasmPath).buffer;
// console.log("nodeFsBuffer: ", nodeFsBuffer);
// myOniguruma.loadWASM(nodeFsBuffer) // this works fine
// myOniguruma.loadWASM(vscodeFsBuffer) // this does not work
myOniguruma.loadWASM(myUint8Array) // ??????????
.then((x) => {
console.log("successful");
})
.catch((error) => {
console.log("error: ", error);
});
});
*/
// vscode.workspace.fs.readFile(thisFileUri).then(function (response) {
// const txt = Utf8ArrayToStr(response);
// const txt2 = ab2str(response, "utf8");
// const buf777 = fromHexString(txt);
// console.log("-text2-:");
// console.log(txt2);
// const onigurumaWasm = fs.readFileSync(wasmPath8).buffer;
// console.log("onigurumaWasm text:::::");
// console.log(ab2str(onigurumaWasm, "utf8"));
// oni2.loadWASM(onigurumaWasm)
// .then((x) => {
// console.log("daloadda");
// })
// .catch((error) => {
// console.log("errrrrrr:");
// console.log(error);
// });
// // console.log("buffer2: ");
// // console.log(buffer2);
// });
// oni2.loadWASM(onigurumaWasm);
export class TextMateLoader {
public readonly scopeNameToLanguage = new Map<string, string>();
private readonly scopeNameToPath = new Map<string, string>();
private readonly languageToScopeName = new Map<string, string>();
private readonly languageToConfigPathOrUri_Ext = new Map<string, vscode.Uri>();
private languageId = 1;
private readonly vsctm: any;
private readonly oniguruma: any;
private readonly languageConfigs = new Map<string, LanguageConfig>();
constructor() {
this.initializeGrammars(); // ::-:
this.vsctm = this.loadTextMate();
this.oniguruma = this.loadOniguruma();
}
public tryGetLanguageConfig(languageID: string) {
const existingTokenizer = this.languageConfigs.get(languageID);
console.log("existingTokenizer"); //aazz
console.log(existingTokenizer); //aazz
if (existingTokenizer) {
return existingTokenizer;
}
const scopeName = this.languageToScopeName.get(languageID);
if (!scopeName) {
console.log("return111", languageID, scopeName);
return;
}
// vscode.Uri.joinPath(extension.extensionUri, pathInsideExtension)
const ext = this.languageToConfigPathOrUri_Ext.get(languageID);
if (!ext) {
console.log("return22222"); //aazz!
return;
}
// const configPathUUU1 = vscode.Uri.joinPath(ext.extensionUri, ext.extensionUri.path);
const configPathUUU1 = ext;
console.log("configPathUUU1:", configPathUUU1);
// const u1 = this.languageToConfigPath.get(languageID);
return new Promise((resolve, reject) => {
// nodeFs.readFile(configPath, (error, content) => {
// if (error) {
// reject(error);
// } else {
// console.log("content:", content); // ::-:
// const config = JSON5.parse(content.toString());
// const brackets = (config as any).brackets as [string[]];
// resolve(brackets);
// }
// });
try {
// const myConfUri = vscode.Uri.file(configPathUUU1);
const myConfUri = configPathUUU1;
// console.log("myConfUri.path1", myConfUri.path);
console.log("daviwyeReeaaad");
vscode.workspace.fs.readFile(myConfUri).then((myUint8Array) => {
console.log("morchaReeaaad");
console.log(myUint8Array);
const contents = new TextDecoder().decode(myUint8Array);
// console.log("contents");
// console.log(contents);
// const vscodeFsBuffer = myUint8Array.buffer;
// const config = JSON5.parse(myUint8Array.toString());
const config = JSON5.parse(contents);
const brackets = (config as any).brackets as [string[]];
// console.log("bracketsInAssync"); //aazz
// console.log(brackets); //aazz
resolve(brackets);
// return 5;
// resolve([]); // ::-:zzzz
});
} catch (error) {
console.log("rrrrrr"); //aazz
console.log(error); //aazz
reject(error);
}
}).then(async (brackets: any) => {
if (!brackets) {
return null;
}
// const registry = new this.vsctm.Registry({
const registry = new myTextmate.Registry({
// tslint:disable-next-line:object-literal-shorthand
onigLib: Promise.resolve({
createOnigScanner: (sources: string[]) =>
new myOniguruma.OnigScanner(sources),
createOnigString: (str: string) =>
new myOniguruma.OnigString(str),
}),
loadGrammar: (scopeName: string) => {
const path = this.scopeNameToPath.get(scopeName);
console.log("pathiiiii");
console.log(path);
if (!path) {
return new Promise((resolve, reject) => resolve(null));
}
return new Promise((resolve, reject) => {
// nodeFs.readFile(path, (error, content) => {
// if (error) {
// reject(error);
// } else {
// const text = content.toString();
// const rawGrammar = this.vsctm.parseRawGrammar(
// text,
// path,
// );
// resolve(rawGrammar);
// }
// });
try {
// const myConfUri = vscode.Uri.file(configPathUUU1);
const myConfUri = configPathUUU1;
// myConfUri = myConfUri.with({ scheme: 'zyidentifier', authority: 'mount', path: `/${myConfUri.authority}` });
console.log("meoreReadStart");
vscode.workspace.fs.readFile(myConfUri).then((myUint8Array) => {
console.log("meoreReadEnd");
// const vscodeFsBuffer = myUint8Array.buffer;
const contents = new TextDecoder().decode(myUint8Array);
// const jjj = JSON.parse(contents);
const reString = stripJsonComments(contents);
const superStr = stripJsonTrailingCommas(reString);
// const reString = jsonminify(contents);
// console.log("contents1045");
// console.log(superStr);
// console.log("reString");
// console.log(reString);
// const text = myUint8Array.toString();
const text = superStr;
// const parseRawGrammar001 = parseRawGrammar000;
// const hhhh: IRawGrammar | undefined;
// const rawGrammar = this.vsctm.parseRawGrammar(
// text,
// path,
// );
const rawGrammar = parseRawGrammar000(
text,
path,
);
console.log("rawGrammar");
console.log(rawGrammar);
resolve(rawGrammar);
});
} catch (error) {
reject(error);
}
});
},
});
// Load the JavaScript grammar and any other grammars included by it async.
console.log("scopeName b:");
console.log(scopeName);
return (
registry.loadGrammarWithConfiguration(
scopeName,
this.languageId++,
{},
) as Thenable<IGrammar | undefined | null>
).then((grammar) => {
console.log("grammar");
console.log(grammar);
if (grammar) {
if (!this.languageConfigs.has(languageID)) {
const mappedBrackets = brackets
.map((b: any) => ({ open: b[0], close: b[1] }))
.filter(
(e: any) => e.open !== "<" && e.close !== ">",
);
if (mappedBrackets.length === 0) {
return;
}
const bracketToId = new Map<
string,
{ open: boolean; key: number }
>();
for (let i = 0; i < brackets.length; i++) {
const bracket = brackets[i];
bracketToId.set(bracket[0], { open: true, key: i });
bracketToId.set(bracket[1], {
open: false,
key: i,
});
}
let maxBracketLength = 0;
for (const bracket of mappedBrackets) {
maxBracketLength = Math.max(
maxBracketLength,
bracket.open.length,
);
maxBracketLength = Math.max(
maxBracketLength,
bracket.close.length,
);
}
const regex = getRegexForBrackets(mappedBrackets);
this.languageConfigs.set(
languageID,
new LanguageConfig(grammar, regex, bracketToId),
);
}
}
return grammar;
});
}).catch((err) => {
console.log("axali catchi:", err);
});
}
private getNodeModulePath(moduleName: string) {
const ppp = path.join(
vscode.env.appRoot,
"node_modules.asar",
moduleName,
);
// console.log("ppp:", ppp);
return ppp;
}
private getNodeModule(moduleName: string) {
return require(this.getNodeModulePath(moduleName));
}
private loadTextMate(): any {
// return this.getNodeModule("vscode-textmate");
return myTextmate;
}
private loadOniguruma(): any {
// const oniguruma = this.getNodeModule("vscode-oniguruma");
// const oniguruma = myOniguruma;
// const oniguruma = oni;
// const wasmPath = path.join(
// this.getNodeModulePath("vscode-oniguruma"),
// "release",
// "onig.wasm",
// );
// const wasmPath = myWasmPath;
// vscode.workspace.fs.readFile(thisFileUri).then((myUint8Array) => {
// const buffer2 = myUint8Array.buffer;
// vvOnig.value.loadWASM(buffer2).then((x) => {
// console.log("morchaa");
// console.log(x);
// console.log("kiiiiiiiii");
// });
// console.log("ba2: ", buffer2);
// // console.log(buffer2);
// });
// const onigurumaWasm = fs.readFileSync(wasmPath).buffer;
// oniguruma.loadWASM(onigurumaWasm);
// return oniguruma;
return myOniguruma;
}
private initializeGrammars() {
// console.log("vscode.extensions.all"); //aazz
// console.log(vscode.extensions.all); //aazz
for (const extension of vscode.extensions.all ) {
const packageJSON = extension.packageJSON as IExtensionPackage;
// console.log("!!!!!!!");
// console.log(!!packageJSON.contributes?.languages);
// console.log(!!packageJSON.contributes?.grammars);
if (packageJSON.contributes) {
if (
packageJSON.contributes.grammars &&
packageJSON.contributes.languages
) {
for (const grammar of packageJSON.contributes.grammars) {
if (
grammar.language &&
grammar.scopeName &&
grammar.path
) {
const fullPath = path.join(
extension.extensionPath,
grammar.path,
);
this.languageToScopeName.set(
grammar.language,
grammar.scopeName,
);
this.scopeNameToPath.set(
grammar.scopeName,
fullPath,
);
this.scopeNameToLanguage.set(
grammar.scopeName,
grammar.language,
);
}
}
for (const language of extension.packageJSON.contributes.languages) {
// console.log("vvvvv---------"); //aazz
// console.log(!!language.configuration, language.id); //aazz
if (language.configuration) {
// console.log("extension::::", extension);
const configPath = path.join(
extension.extensionPath,
language.configuration,
);
const uuu = vscode.Uri.joinPath(extension.extensionUri, language.configuration);
// console.log("pppppkkk", configPath); //aazz
this.languageToConfigPathOrUri_Ext.set(
language.id,
// configPath,
// uuu
uuu
);
}
}
}
}
}
}
}
export default TextMateLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment