~damien/jedit-lsp

44beae3bc4a781ec2b25d7cadc0c5add476064a7 — Damien Radtke 11 months ago ee8ebfa
Use root-files property rather than get-root-uri script
4 files changed, 42 insertions(+), 40 deletions(-)

M LSP.props
M lsp/Server.java
M lsp/ServerDefinition.java
M lsp/Utils.java
M LSP.props => LSP.props +3 -1
@@ 67,11 67,13 @@ messages.lsp.mode-settings-description=<html><b>Assign LSP servers to modes</b><
lsp.servers=gopls jdtls
lsp.server.gopls.command=gopls
lsp.server.gopls.install=exec("go install golang.org/x/tools/gopls@latest");
lsp.server.gopls.root-files=go.work go.mod

lsp.server.jdtls.command=${PLUGIN_HOME}/jdtls/bin/jdtls
lsp.server.jdtls.install=dir = lsp.Utils.getPluginSubdir("jdtls"); \
                         eclipseDownload(view, "jdtls", "1.23.0", dir); \
                         new File(dir, "bin/jdtls").setExecutable(true);
lsp.server.jdtls.get-root-uri=lsp.Utils.findRootURI(buffer, new String[]{ "build.xml", "pom.xml", "settings.gradle", "settings.gradle.kts" })
lsp.server.jdtls.root-files=build.xml pom.xml settings.gradle settings.gradle.kts - build.gradle build.gradle.kts

# Map a mode to the language server to use
mode.go.languageServer=gopls

M lsp/Server.java => lsp/Server.java +4 -24
@@ 237,31 237,11 @@ public class Server {
		if (!roots.isEmpty()) {
			return roots;
		}
		if (this.serverDefinition.hasGetRootURI()) {
			String script = this.serverDefinition.getGetRootURI();
			log(Log.WARNING, "Executing get-root-uri script: " + script);
			try {
				NameSpace ns = new NameSpace(BeanShell.getNameSpace(), "LSP " + this.getName() + " findWorkspaceFolders()");
				ns.setVariable("buffer", buffer);
				Object result = BeanShell.eval(null, ns, script);
				if (result instanceof String) {
					return Collections.singletonList((String) result);
				} else if ((result instanceof Path) || (result instanceof URI) || (result instanceof URL)) {
					return Collections.singletonList(result.toString());
				} else if (result instanceof List) {
					List<String> convertedResult = new ArrayList<>();
					for (Object item : ((List) result)) {
						convertedResult.add(item.toString());
					}
					return convertedResult;
				} else {
					log(Log.WARNING, "get-root-uri script returned an invalid result: " + result.toString());
				}
			} catch (UtilEvalError e) {
				log(Log.ERROR, "get-root-uri script threw an error", e);
		for (List<String> rootFileGroup : this.serverDefinition.getRootFiles()) {
			String root = Utils.findRootURI(buffer, rootFileGroup);
			if (root != null) {
				return Collections.singletonList(root);
			}
		} else {
			log(Log.WARNING, "no get-root-uri script");
		}
		return Collections.emptyList();
	}

M lsp/ServerDefinition.java => lsp/ServerDefinition.java +31 -15
@@ 1,5 1,9 @@
package lsp;

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.util.Log;



@@ 7,7 11,7 @@ public class ServerDefinition implements Comparable<ServerDefinition> {
	private String name;
	private String rawCommand;
	private String installScript;
	private String getRootURI;
	private List<List<String>> rootFiles;

	public ServerDefinition(String name) {
		this.name = name;


@@ 19,17 23,37 @@ public class ServerDefinition implements Comparable<ServerDefinition> {
		if (this.installScript == null) {
			this.installScript = "";
		}
		this.getRootURI = jEdit.getProperty(getProp("get-root-uri"));
		if (this.getRootURI == null) {
			this.getRootURI = "";
		this.rootFiles = parseRootFiles(jEdit.getProperty(getProp("root-files")));
	}

	private static List<List<String>> parseRootFiles(String raw) {
		if (raw == null || raw.equals("")) {
			return Collections.emptyList();
		}

		List<List<String>> result = new ArrayList<>();

		List<String> group = new ArrayList<>();
		for (String file : raw.split(" ")) {
			if (file.equals("-")) {
				result.add(group);
				group = new ArrayList<>();
			} else {
				group.add(file);
			}
		}

		if (!group.isEmpty()) {
			result.add(group);
		}
		return result;
	}

	public void save() {
		Log.log(Log.DEBUG, this, "Saving command as: " + this.rawCommand);
		jEdit.setProperty(getProp("command"), this.rawCommand);
		jEdit.setProperty(getProp("install"), this.installScript);
		jEdit.setProperty(getProp("get-root-uri"), this.getRootURI);
		//jEdit.setProperty(getProp("root-files"), this.rootFiles);
	}

	public String getName() {


@@ 62,16 86,8 @@ public class ServerDefinition implements Comparable<ServerDefinition> {
		this.installScript = installScript;
	}

	public String getGetRootURI() {
		return this.getRootURI;
	}

	public void setGetRootURI(String getRootURI) {
		this.getRootURI = getRootURI;
	}

	public boolean hasGetRootURI() {
		return this.getRootURI != null && !this.getRootURI.isEmpty();
	public List<List<String>> getRootFiles() {
		return this.rootFiles;
	}

	@Override

M lsp/Utils.java => lsp/Utils.java +4 -0
@@ 378,6 378,10 @@ public class Utils {
		return findRootURI(buffer, new String[]{file});
	}

	public static String findRootURI(Buffer buffer, List<String> files) {
		return findRootURI(buffer, files.toArray(new String[0]));
	}

	public static String findRootURI(Buffer buffer, String[] files) {
		return findRootURI(buffer.getVFS(), buffer.getDirectory(), (vfs, session, view, dir) -> {
			try {