Skip to content
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

Improve plugin start #238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Currently supports text document level events and diagnostics.

# Install VSCode plugin

`Ralph LSP` is available on the [VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=alephium.ralph-lsp).

### Manual Installation

Follow these steps:

1. Download: Get the `.vsix` plugin file from the [latest release](https://github.com/alephium/ralph-lsp/releases/latest).
Expand Down Expand Up @@ -52,6 +56,23 @@ lazy_plugin_config = {

The plugin adds file type detection, syntax highlighting and start the LSP server, make sure you have `ralph-lsp` available in your `PATH`

# Integrate new editor

Please help us integrate Ralph LSP into your favorite editor. We are happy to assist you with any questions you may have.

You can either create your own repository or add a new folder in this repository following the `plugin-<editor>` naming convention.

### Define Root Directory

It's important to ensure that your plugin can find the correct root directory for your Ralph project.
For example, if you open your editor while inside the `contracts` folder, the plugin should be able to identify the root directory as `../contracts`.

In our current plugins, we follow this heuristic:

* Look for the presence of either `alephium.config.ts`, `contracts`, or `.ralph.json`.

If none of these are found, the plugin will use the current directory as the root directory.

# Build the JAR

```shell
Expand Down
2 changes: 1 addition & 1 deletion plugin-nvim/plugin/ralph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local function ralph_init()

capabilities.workspace.didChangeWatchedFiles.dynamicRegistration = true

local root_dir = vim.fs.dirname(vim.fs.find({'build.ralph', 'contracts', 'artifacts'}, { upward = true })[1])
local root_dir = vim.fs.dirname(vim.fs.find({'alephium.config.ts', 'contracts', '.ralph-lsp'}, { upward = true })[1])

if root_dir == nil then root_dir = vim.fn.getcwd() end

Expand Down
43 changes: 40 additions & 3 deletions plugin-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {ExtensionContext} from 'vscode';
import {ExtensionContext, Uri, workspace, } from 'vscode';

import {LanguageClient, LanguageClientOptions, ServerOptions} from 'vscode-languageclient/node';
import * as path from "node:path";
import * as fs from 'fs';


let client: LanguageClient;

Expand Down Expand Up @@ -32,6 +34,22 @@ function build_args(release: boolean) {
export function activate(context: ExtensionContext) {
console.log("Activating Ralph LSP client");

const rootFiles = ['alephium.config.ts', 'contracts', '.ralph-lsp'];

const workspaceFolders = workspace.workspaceFolders;

let rootDir: string | null = null;

if (workspaceFolders) {
for (const folder of workspaceFolders) {
const potentialRootDir = findRootDir(folder.uri.fsPath, rootFiles);
if (potentialRootDir) {
rootDir = potentialRootDir;
break;
}
}
}

const args =
build_args(true);

Expand All @@ -43,8 +61,9 @@ export function activate(context: ExtensionContext) {
const clientOptions: LanguageClientOptions = {
documentSelector: [
{pattern: '**/*.ral'},
{language: 'json', pattern: '**/ralph.json'},
]
{language: 'json', pattern: '**/ralph.json'}
],
workspaceFolder: rootDir ? { uri: Uri.file(rootDir), name: "ralph-lsp-home", index: 0} : undefined
};

// Create the client and store it.
Expand All @@ -65,3 +84,21 @@ export function deactivate(): Thenable<void> | undefined {
}
return client.stop();
}

// Helper function to recursively find the root directory
function findRootDir(currentDir: string, rootFiles: string[]): string | null {

for (const file of rootFiles) {
if (fs.existsSync(path.join(currentDir, file))) {
return currentDir;
}
}

const parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
// Reached the root directory
return null;
}

return findRootDir(parentDir, rootFiles);
}