diff --git a/coc-mlir/.npmignore b/coc-mlir/.npmignore new file mode 100644 index 0000000..de2bf00 --- /dev/null +++ b/coc-mlir/.npmignore @@ -0,0 +1,4 @@ +/* +!/out/ +*.map +!/prebuilds/ diff --git a/coc-mlir/README.md b/coc-mlir/README.md new file mode 100644 index 0000000..635c163 --- /dev/null +++ b/coc-mlir/README.md @@ -0,0 +1,32 @@ +# MLIR + +Ported from [vscode-mlir](https://github.com/llvm/vscode-mlir). + +Because some APIs of [vscode](github.com/microsoft/vscode) are missing in +[coc.nvim](https://github.com/neoclide/coc.nvim), disable some features +temporarily: + +- command `mlir.viewPDLLOutput`: miss + `vscode.workspace.openTextDocument({language, content})` +- custom editor `mlir.bytecode`: miss + `vscode.window.registerCustomEditorProvider()` +- filesystem `mlir.bytecode-mlir`: miss `vscode.workspace.registerFileSystemProvider()` +- use `chokidar.watch()` to detect the changes of the paths of language + servers: miss `vscode.Disposable.constructor()` + +## Install + +- [coc-marketplace](https://github.com/fannheyward/coc-marketplace) +- [npm](https://www.npmjs.com/package/coc-mlir) +- vim: + +```vim +" command line +CocInstall coc-mlir +" or add the following code to your vimrc +let g:coc_global_extensions = ['coc-mlir', 'other coc-plugins'] +``` + +## Usage + +Refer [vscode-mlir](https://github.com/llvm/vscode-mlir). diff --git a/coc-mlir/package.json b/coc-mlir/package.json new file mode 100644 index 0000000..58edd09 --- /dev/null +++ b/coc-mlir/package.json @@ -0,0 +1,213 @@ +{ + "name": "coc-mlir", + "displayName": "MLIR", + "description": "MLIR Language Extension", + "version": "0.0.11", + "publisher": "llvm-vs-code-extensions", + "homepage": "https://mlir.llvm.org/", + "icon": "icon.png", + "engines": { + "coc": "^0.0.82" + }, + "categories": [ + "Programming Languages" + ], + "keywords": [ + "coc.nvim", + "LLVM", + "MLIR", + "PDLL", + "TableGen", + "tblgen", + "tablegen" + ], + "activationEvents": [ + "onFileSystem:mlir.bytecode-mlir", + "onCustomEditor:mlir.bytecode", + "onLanguage:mlir", + "onLanguage:pdll", + "onLanguage:tablegen" + ], + "main": "./out/extension.js", + "scripts": { + "patch": "scripts/patch.sh ../src/*.ts", + "prepack": "npm run patch && tsc -p ./", + "compile": "tsc -watch -p ./", + "format": "clang-format -i --glob=\"{src,test}/*.ts\"", + "package": "vsce package", + "git-clang-format": "git-clang-format" + }, + "dependencies": { + "base64-js": "^1.5.1", + "chokidar": "3.5.2" + }, + "devDependencies": { + "@types/mocha": "^7.0.2", + "@types/node": "^14.17.0", + "clang-format": "^1.8.0", + "coc.nvim": "^0.0.83-next.18", + "typescript": "^4.6.4" + }, + "repository": { + "type": "git", + "url": "https://github.com/llvm/vscode-mlir" + }, + "contributes": { + "customEditors": [ + { + "viewType": "mlir.bytecode", + "displayName": "MLIR Bytecode", + "priority": "default", + "selector": [ + { + "filenamePattern": "*.mlirbc" + } + ] + } + ], + "languages": [ + { + "id": "mlir", + "aliases": [ + "MLIR", + "mlir" + ], + "extensions": [ + ".mlir", + ".mlirbc" + ], + "configuration": "./language-configuration.json" + }, + { + "id": "mlir-injection" + }, + { + "id": "pdll", + "aliases": [ + "PDLL", + "pdll" + ], + "extensions": [ + ".pdll" + ], + "configuration": "./pdll-language-configuration.json" + }, + { + "id": "tablegen", + "aliases": [ + "TableGen", + "tblgen" + ], + "extensions": [ + ".td" + ], + "configuration": "./tablegen-language-configuration.json" + } + ], + "grammars": [ + { + "language": "mlir", + "scopeName": "source.mlir", + "path": "./grammar.json" + }, + { + "language": "mlir-injection", + "scopeName": "markdown.mlir.codeblock", + "path": "markdown-grammar.json", + "injectTo": [ + "text.html.markdown" + ], + "embeddedLanguages": { + "meta.embedded.block.mlir": "mlir", + "meta.embedded.block.pdll": "pdll", + "meta.embedded.block.tablegen": "tablegen" + } + }, + { + "scopeName": "source.cpp.mlir", + "path": "./cpp-grammar.json", + "injectTo": [ + "source.cpp" + ], + "embeddedLanguages": { + "source.mlir": "mlir" + } + }, + { + "language": "pdll", + "scopeName": "source.pdll", + "path": "./pdll-grammar.json" + }, + { + "language": "tablegen", + "scopeName": "source.tablegen", + "path": "./tablegen-grammar.json" + } + ], + "configuration": { + "type": "object", + "title": "MLIR", + "properties": { + "mlir.server_path": { + "scope": "resource", + "type": "string", + "description": "The file path of the mlir-lsp-server executable." + }, + "mlir.pdll_server_path": { + "scope": "resource", + "type": "string", + "description": "The file path of the mlir-pdll-lsp-server executable." + }, + "mlir.pdll_compilation_databases": { + "scope": "resource", + "type": "array", + "description": "A list of `pdll_compile_commands.yml` database files containing information about .pdll files processed by the server." + }, + "mlir.tablegen_server_path": { + "scope": "resource", + "type": "string", + "description": "The file path of the tblgen-lsp-server executable." + }, + "mlir.tablegen_compilation_databases": { + "scope": "resource", + "type": "array", + "description": "A list of `tablegen_compile_commands.yml` database files containing information about .td files processed by the server." + }, + "mlir.onSettingsChanged": { + "type": "string", + "default": "prompt", + "description": "Action taken when a setting change requires a server restart to take effect.", + "enum": [ + "prompt", + "restart", + "ignore" + ], + "enumDescriptions": [ + "Prompt the user for restarting the server", + "Automatically restart the server", + "Do nothing" + ] + } + } + }, + "commands": [ + { + "command": "mlir.restart", + "title": "mlir: Restart language server" + }, + { + "command": "mlir.viewPDLLOutput", + "title": "mlir-pdll: View PDLL output" + } + ], + "menus": { + "editor/context": [ + { + "command": "mlir.viewPDLLOutput", + "group": "z_commands", + "when": "editorLangId == pdll" + } + ] + } + } +} diff --git a/coc-mlir/scripts/patch.pl b/coc-mlir/scripts/patch.pl new file mode 100755 index 0000000..5363953 --- /dev/null +++ b/coc-mlir/scripts/patch.pl @@ -0,0 +1,2 @@ +#!/usr/bin/env -S perl -p +s=//#==; diff --git a/coc-mlir/scripts/patch.sh b/coc-mlir/scripts/patch.sh new file mode 100755 index 0000000..4197d23 --- /dev/null +++ b/coc-mlir/scripts/patch.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -e +cd "$(dirname "$(dirname "$(readlink -f "$0")")")" + +for file; do + scripts/patch.pl "$file" | cpp -DHAVE_COC_NVIM -xassembler-with-cpp -nostdinc -P -C -o"${file#*/}" +done diff --git a/coc-mlir/src/.gitignore b/coc-mlir/src/.gitignore new file mode 100644 index 0000000..d3a4cb6 --- /dev/null +++ b/coc-mlir/src/.gitignore @@ -0,0 +1,3 @@ +# all ts files are generated by `npm run patch` +* +!.gitignore diff --git a/coc-mlir/tsconfig.json b/coc-mlir/tsconfig.json new file mode 100644 index 0000000..57afb74 --- /dev/null +++ b/coc-mlir/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig", + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "outDir": "out", + "rootDir": "src" + }, + "include": [ + "src" + ], +} diff --git a/src/command.ts b/src/command.ts index 4623a5b..512a068 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,3 +1,4 @@ +//# #if HAVE_VSCODE import * as vscode from 'vscode'; import {MLIRContext} from './mlirContext'; @@ -23,3 +24,4 @@ export abstract class Command extends vscode.Disposable { */ abstract execute(...args: any[]): any; } +//# #endif diff --git a/src/config.ts b/src/config.ts index 04ff8f5..aee3262 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,8 @@ +//# #if HAVE_VSCODE import * as vscode from 'vscode'; +//# #elif HAVE_COC_NVIM +//# import * as vscode from 'coc.nvim'; +//# #endif /** * Gets the config value `mlir.`, with an optional workspace folder. diff --git a/src/configWatcher.ts b/src/configWatcher.ts index 6842687..a764297 100644 --- a/src/configWatcher.ts +++ b/src/configWatcher.ts @@ -1,5 +1,9 @@ import * as chokidar from 'chokidar'; +//# #if HAVE_VSCODE import * as vscode from 'vscode'; +//# #elif HAVE_COC_NVIM +//# import * as vscode from 'coc.nvim'; +//# #endif import * as config from './config'; import {MLIRContext} from './mlirContext'; @@ -71,6 +75,7 @@ export async function activate( } // If the server path actually exists, track it in case it changes. + //# #if HAVE_VSCODE const fileWatcher = chokidar.watch(serverPath, fileWatcherConfig); fileWatcher.on('all', (event, _filename, _details) => { if (event != 'unlink') { @@ -81,5 +86,6 @@ export async function activate( }); mlirContext.subscriptions.push( new vscode.Disposable(() => { fileWatcher.close(); })); + //# #endif } } diff --git a/src/extension.ts b/src/extension.ts index 133fb8f..56d0f9f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,8 +1,13 @@ +//# #if HAVE_VSCODE import * as vscode from 'vscode'; - import {registerMLIRExtensions} from './MLIR/mlir'; -import {MLIRContext} from './mlirContext'; +// https://github.com/neoclide/coc.nvim/discussions/4918 import {registerPDLLExtensions} from './PDLL/pdll'; +//# #elif HAVE_COC_NVIM +//# import * as vscode from 'coc.nvim'; +//# #endif + +import {MLIRContext} from './mlirContext'; /** * This method is called when the extension is activated. The extension is @@ -22,8 +27,10 @@ export function activate(context: vscode.ExtensionContext) { mlirContext.dispose(); await mlirContext.activate(outputChannel); })); + //# #if HAVE_VSCODE registerMLIRExtensions(context, mlirContext); registerPDLLExtensions(context, mlirContext); + //# #endif mlirContext.activate(outputChannel); } diff --git a/src/mlirContext.ts b/src/mlirContext.ts index c7b6de6..39f5c39 100644 --- a/src/mlirContext.ts +++ b/src/mlirContext.ts @@ -1,7 +1,12 @@ import * as fs from 'fs'; import * as path from 'path'; +//# #if HAVE_VSCODE import * as vscode from 'vscode'; import * as vscodelc from 'vscode-languageclient/node'; +//# #elif HAVE_COC_NVIM +//# import * as vscode from 'coc.nvim'; +//# import * as vscodelc from 'coc.nvim'; +//# #endif import * as config from './config'; import * as configWatcher from './configWatcher'; @@ -37,7 +42,13 @@ export class MLIRContext implements vscode.Disposable { // document. It removes the need to pro-actively start language clients for // every folder within the workspace and every language type we provide. const startClientOnOpenDocument = async (document: vscode.TextDocument) => { - await this.getOrActivateLanguageClient(document.uri, document.languageId); + await this.getOrActivateLanguageClient( + //# #if HAVE_VSCODE + document.uri, + //# #elif HAVE_COC_NVIM + //# vscode.Uri.parse(document.uri), + //# #endif + document.languageId); }; // Process any existing documents. for (const textDoc of vscode.workspace.textDocuments) { @@ -118,7 +129,12 @@ export class MLIRContext implements vscode.Disposable { // 'build' directory within the current workspace. if (databases.length === 0) { if (workspaceFolder) { - databases.push(workspaceFolder.uri.fsPath + + databases.push( + //# #if HAVE_VSCODE + workspaceFolder.uri.fsPath + + //# #elif HAVE_COC_NVIM + //# vscode.Uri.parse(workspaceFolder.uri).fsPath + + //# #endif `/build/${languageName}_compile_commands.yml`); } @@ -224,6 +240,7 @@ export class MLIRContext implements vscode.Disposable { return [ null, serverPath ]; } + //# #if HAVE_VSCODE // Check that the file actually exists. if (!fs.existsSync(serverPath)) { vscode.window @@ -240,6 +257,7 @@ export class MLIRContext implements vscode.Disposable { }); return [ null, serverPath ]; } + //# #endif // Configure the server options. const serverOptions: vscodelc.ServerOptions = { @@ -252,7 +270,13 @@ export class MLIRContext implements vscode.Disposable { let selectorPattern: string = null; if (workspaceFolder) { filePattern = new vscode.RelativePattern(workspaceFolder, filePattern); - selectorPattern = `${workspaceFolder.uri.fsPath}/**/*`; + selectorPattern = `${ +//# #if HAVE_VSCODE +workspaceFolder.uri +//# #elif HAVE_COC_NVIM +//# vscode.Uri.parse(workspaceFolder.uri) +//# #endif +.fsPath}/**/*`; } // Configure the middleware of the client. This is sort of abused to allow