~damien/jedit-lsp

9615a34fe9a20fc2653499f0e4eb95080d70866d — Damien Radtke 4 months ago 6a31cc8
First attempt at loading workspace settings
3 files changed, 102 insertions(+), 0 deletions(-)

M lsp/Capabilities.java
A lsp/Settings.java
M lsp/jEditLanguageClient.java
M lsp/Capabilities.java => lsp/Capabilities.java +1 -0
@@ 15,6 15,7 @@ public class Capabilities {
		DidChangeWatchedFilesCapabilities didChangeWatchedFilesCapabilities = new DidChangeWatchedFilesCapabilities();
		didChangeWatchedFilesCapabilities.setDynamicRegistration(true);
		workspaceClient.setDidChangeWatchedFiles(didChangeWatchedFilesCapabilities);
		workspaceClient.setConfiguration(true);

		WorkspaceEditCapabilities workspaceEdit = new WorkspaceEditCapabilities();
		workspaceEdit.setResourceOperations(Arrays.asList("create", "rename", "delete"));

A lsp/Settings.java => lsp/Settings.java +65 -0
@@ 0,0 1,65 @@
package lsp;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

import org.gjt.sp.util.Log;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

public class Settings {
	private Map<String, Object> settings;

	public Settings(File file) throws IOException {
		this.settings = new HashMap<>();
		JsonObject root = JsonParser.parseReader(new FileReader(file)).getAsJsonObject();
		load(root, null);
	}

	private void load(JsonObject object, String prefix) {
		for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
			String key = entry.getKey();
			String fullKey = prefix == null ? key : prefix + "." + key;
			JsonElement value = entry.getValue();

			if (value.isJsonPrimitive()) {
				JsonPrimitive primitive = value.getAsJsonPrimitive();
				if (primitive.isString()) {
					this.settings.put(fullKey, primitive.getAsString());
				} else if (primitive.isNumber()) {
					this.settings.put(fullKey, primitive.getAsNumber());
				} else if (primitive.isBoolean()) {
					this.settings.put(fullKey, primitive.getAsBoolean());
				} else {
					Log.log(Log.WARNING, this, "unsupported JSON primitive for key " + fullKey + ": " + primitive.toString());
				}
			} else if (value.isJsonObject()) {
				load(value.getAsJsonObject(), fullKey);
			} else {
				Log.log(Log.WARNING, this, "unsupported value for key " + fullKey + ": " + value.toString());
			}
		}
	}

	public Map<String, Object> getSettings() {
		return this.settings;
	}

	public Map<String, Object> getSettingsForScope(String section) {
		String prefix = section + ".";
		Map<String, Object> sectionSettings = new HashMap<>();
		for (Map.Entry<String, Object> entry : this.settings.entrySet()) {
			String key = entry.getKey();
			if (key.startsWith(prefix)) {
				sectionSettings.put(key.substring(prefix.length()), entry.getValue());
			}
		}
		return sectionSettings;
	}
}

M lsp/jEditLanguageClient.java => lsp/jEditLanguageClient.java +36 -0
@@ 2,6 2,7 @@ package lsp;

import lsp.painter.DiagnosticsPainter;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


@@ 32,6 33,8 @@ import org.gjt.sp.jedit.io.VFSManager;
import org.gjt.sp.util.TaskManager;
import org.gjt.sp.util.ThreadUtilities;

import org.eclipse.lsp4j.ConfigurationItem;
import org.eclipse.lsp4j.ConfigurationParams;
import org.eclipse.lsp4j.CreateFile;
import org.eclipse.lsp4j.CreateFileOptions;
import org.eclipse.lsp4j.DeleteFile;


@@ 60,6 63,7 @@ import org.eclipse.lsp4j.WorkDoneProgressEnd;
import org.eclipse.lsp4j.WorkDoneProgressNotification;
import org.eclipse.lsp4j.WorkDoneProgressReport;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;



@@ 298,6 302,38 @@ public class jEditLanguageClient implements LanguageClient {
		}
	}

	@Override
	public CompletableFuture<List<Object>> configuration(ConfigurationParams params) {
		Log.log(Log.DEBUG, this, "configuration: " + params.toString());
		CompletableFuture<List<Object>> future = new CompletableFuture<>();
		ThreadUtilities.runInBackground(() -> {
			// TODO: iterate over all workspace folders, and merge the results
			List<Object> config = new ArrayList<>(params.getItems().size());
			List<WorkspaceFolder> folders = this.server.getWorkspaceFolders();
			if (!folders.isEmpty()) {
				// TODO: use VFS?
				String rootPath = folders.get(0).getUri();
				if (rootPath.startsWith("file://")) {
					rootPath = rootPath.substring(7);
				}
				try {
					Settings settings = new Settings(new File(rootPath, ".vscode/settings.json"));
					for (ConfigurationItem item : params.getItems()) {
						config.add(settings.getSettingsForScope(item.getSection()));
					}
				} catch (IOException e) {
					Log.log(Log.ERROR, this, "Error loading configuration settings:", e);
				}
			} else {
				for (ConfigurationItem item : params.getItems()) {
					config.add(null);
				}
			}
			future.complete(config);
		});
		return future;
	}

	public Map<String, Collection<Watcher>> getWatchers() {
		return this.registeredWatchers;
	}