Skip to content

Commit

Permalink
Qute support for multi module project
Browse files Browse the repository at this point in the history
Fixes #930

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 21, 2023
1 parent e944bd1 commit 1ebac0a
Show file tree
Hide file tree
Showing 29 changed files with 683 additions and 114 deletions.
1 change: 1 addition & 0 deletions qute.jdt/com.redhat.qute.jdt/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<!-- Delegate command handler for Qute template -->
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
<delegateCommandHandler class="com.redhat.qute.jdt.internal.ls.QuteSupportForTemplateDelegateCommandHandler">
<command id="qute/template/projects"/>
<command id="qute/template/project"/>
<command id="qute/template/projectDataModel"/>
<command id="qute/template/userTags"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package com.redhat.qute.commons;

import java.util.List;

/**
* Project information where a Qute template belongs to.
*
Expand All @@ -23,11 +25,14 @@ public class ProjectInfo {

private String templateBaseDir;

private List<String> projectDependencyUris;

public ProjectInfo() {
}

public ProjectInfo(String uri, String templateBaseDir) {
setUri(uri);
public ProjectInfo(String projectUri, List<String> projectDependencies, String templateBaseDir) {
setUri(projectUri);
setProjectDependencyUris(projectDependencies);
setTemplateBaseDir(templateBaseDir);
}

Expand All @@ -49,6 +54,24 @@ public void setUri(String uri) {
this.uri = uri;
}

/**
* Returns the project dependency Uris.
*
* @return the project dependency Uris.
*/
public List<String> getProjectDependencyUris() {
return projectDependencyUris;
}

/**
* Set the project dependency Uris.
*
* @param projectDependencyUris the project dependency Uris.
*/
public void setProjectDependencyUris(List<String> projectDependencyUris) {
this.projectDependencyUris = projectDependencyUris;
}

/**
* Returns the Qute templates base directory and null otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -99,6 +100,28 @@ public static QuteSupportForTemplate getInstance() {
return INSTANCE;
}

/**
* Returns the list of Qute projects from the workspace.
*
* @param utils the JDT LS utility.
* @param monitor the progress monitor.
*
* @return the list of Qute projects from teh workspace.
*/
public List<ProjectInfo> getProjects(IJDTUtils utils, IProgressMonitor monitor) {
List<ProjectInfo> quteProjects = new ArrayList<>();
IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
// Loop for project from the workspace
for (IProject project : allProjects) {
IJavaProject javaProject = getJavaProject(project);
if (isQuteProject(javaProject)) {
// It is a Qute project
quteProjects.add(JDTQuteProjectUtils.getProjectInfo(javaProject));
}
}
return quteProjects;
}

/**
* Returns the project information for the given project Uri.
*
Expand Down Expand Up @@ -405,6 +428,19 @@ private static IProject findProject(String resourceUri, IJDTUtils utils) {
*/
private static boolean isQuteProject(IProject project) {
IJavaProject javaProject = getJavaProject(project);
return isQuteProject(javaProject);
}

/**
* Returns true if the given Java project is a Java Qute project and false
* otherwise.
*
* @param project the Java project.
*
* @return true if the given Java project is a Java Qute project and false
* otherwise.
*/
private static boolean isQuteProject(IJavaProject javaProject) {
if (javaProject == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class QuteSupportForTemplateDelegateCommandHandler extends AbstractQuteDe

private static final String CLASS_NAME_ATTR = "className";

private static final String QUTE_TEMPLATE_PROJECTS_COMMAND_ID = "qute/template/projects";
private static final String QUTE_TEMPLATE_PROJECT_COMMAND_ID = "qute/template/project";

private static final String QUTE_TEMPLATE_PROJECT_DATA_MODEL_COMMAND_ID = "qute/template/projectDataModel";
Expand All @@ -93,6 +94,8 @@ public class QuteSupportForTemplateDelegateCommandHandler extends AbstractQuteDe
@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
switch (commandId) {
case QUTE_TEMPLATE_PROJECTS_COMMAND_ID:
return getProjects(commandId, monitor);
case QUTE_TEMPLATE_PROJECT_COMMAND_ID:
return getProjectInfo(arguments, commandId, monitor);
case QUTE_TEMPLATE_PROJECT_DATA_MODEL_COMMAND_ID:
Expand All @@ -113,6 +116,10 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
return null;
}

private static List<ProjectInfo> getProjects(String commandId, IProgressMonitor monitor) {
return QuteSupportForTemplate.getInstance().getProjects(JDTUtilsLSImpl.getInstance(), monitor);
}

private static ProjectInfo getProjectInfo(List<Object> arguments, String commandId, IProgressMonitor monitor) {
QuteProjectParams params = createQuteProjectParams(arguments, commandId);
return QuteSupportForTemplate.getInstance().getProjectInfo(params, JDTUtilsLSImpl.getInstance(), monitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
*******************************************************************************/
package com.redhat.qute.jdt.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving;

Expand All @@ -30,6 +37,8 @@
*/
public class JDTQuteProjectUtils {

private static final Logger LOGGER = Logger.getLogger(JDTQuteProjectUtils.class.getName());

private static final String TEMPLATES_BASE_DIR = "src/main/resources/templates/";

private JDTQuteProjectUtils() {
Expand All @@ -40,7 +49,20 @@ public static ProjectInfo getProjectInfo(IJavaProject javaProject) {
IProject project = javaProject.getProject();
String projectUri = getProjectURI(project);
String templateBaseDir = project.getFile(TEMPLATES_BASE_DIR).getLocationURI().toString();
return new ProjectInfo(projectUri, templateBaseDir);
// Project dependencies
List<String> projectDependencies = Collections.emptyList();
try {
String[] requiredProjectNames = javaProject.getRequiredProjectNames();
if (requiredProjectNames != null) {
projectDependencies = Arrays.asList(requiredProjectNames);
}
} catch (JavaModelException e) {
// Should never occurs
LOGGER.log(Level.SEVERE,
"Error while getting project dependencies for '" + javaProject.getElementName() + "' Java project.",
e);
}
return new ProjectInfo(projectUri, projectDependencies, templateBaseDir);
}

/**
Expand Down Expand Up @@ -84,7 +106,8 @@ public static boolean hasQuteSupport(IJavaProject javaProject) {
return JDTTypeUtils.findType(javaProject, QuteJavaConstants.ENGINE_BUILDER_CLASS) != null;
}

public static TemplatePathInfo getTemplatePath(String className, String methodOrFieldName, boolean ignoreFragments) {
public static TemplatePathInfo getTemplatePath(String className, String methodOrFieldName,
boolean ignoreFragments) {
String fragmentId = null;
StringBuilder templateUri = new StringBuilder(TEMPLATES_BASE_DIR);
if (className != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package com.redhat.qute.commons;

import java.util.List;

/**
* Project information where a Qute template belongs to.
*
Expand All @@ -23,11 +25,14 @@ public class ProjectInfo {

private String templateBaseDir;

private List<String> projectDependencyUris;

public ProjectInfo() {
}

public ProjectInfo(String uri, String templateBaseDir) {
setUri(uri);
public ProjectInfo(String projectUri, List<String> projectDependencies, String templateBaseDir) {
setUri(projectUri);
setProjectDependencyUris(projectDependencies);
setTemplateBaseDir(templateBaseDir);
}

Expand All @@ -49,6 +54,24 @@ public void setUri(String uri) {
this.uri = uri;
}

/**
* Returns the project dependency Uris.
*
* @return the project dependency Uris.
*/
public List<String> getProjectDependencyUris() {
return projectDependencyUris;
}

/**
* Set the project dependency Uris.
*
* @param projectDependencyUris the project dependency Uris.
*/
public void setProjectDependencyUris(List<String> projectDependencyUris) {
this.projectDependencyUris = projectDependencyUris;
}

/**
* Returns the Qute templates base directory and null otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.lsp4j.SetTraceParams;
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
import org.eclipse.lsp4j.WorkDoneProgressNotification;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageServer;
Expand Down Expand Up @@ -108,11 +107,12 @@ public class QuteLanguageServer implements LanguageServer, ProcessLanguageServer
private QuteLanguageClientAPI languageClient;

private QuteCapabilityManager capabilityManager;
private List<WorkspaceFolder> workspaceFolders;

public QuteLanguageServer() {
this.sharedSettings = new SharedSettings();
this.projectRegistry = new QuteProjectRegistry(this, this, this, this, this, this, this, this);
this.projectRegistry = new QuteProjectRegistry(this, this, this, this, this, this, this, this, //
() -> capabilityManager.getClientCapabilities()
.isWorkDoneProgressSupported() ? this : null);
this.quteLanguageService = new QuteLanguageService(projectRegistry);
this.textDocumentService = new QuteTextDocumentService(this);
this.workspaceService = new QuteWorkspaceService(this);
Expand All @@ -135,7 +135,6 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {

projectRegistry.setDidChangeWatchedFilesSupported(
capabilityManager.getClientCapabilities().isDidChangeWatchedFilesRegistered());
workspaceFolders = params.getWorkspaceFolders();

InitializeResult initializeResult = new InitializeResult(serverCapabilities);
return CompletableFuture.completedFuture(initializeResult);
Expand Down Expand Up @@ -163,19 +162,13 @@ public void initialized(InitializedParams params) {
* Try to load the Qute project for each workspace folder.
*/
private void loadQuteProjects() {
if (workspaceFolders == null || workspaceFolders.isEmpty()) {
// No workspace folders.
return;
}
CompletableFuture.runAsync(() -> {
for (WorkspaceFolder workspaceFolder : workspaceFolders) {
// Get the LSP client progress support
ProgressSupport progressSupport = capabilityManager.getClientCapabilities()
.isWorkDoneProgressSupported() ? this : null;
// Try to load the Qute project of the current workspace folder
projectRegistry.tryToLoadQuteProject(workspaceFolder, progressSupport);
}
});
getLanguageClient().getProjects()
.thenAccept(projects -> {
if (projects != null && !projects.isEmpty()) {
// There are some Qute projects in the workspace, load them
projectRegistry.loadQuteProjects(projects);
}
});
}

/**
Expand Down Expand Up @@ -267,6 +260,30 @@ public SharedSettings getSharedSettings() {
return sharedSettings;
}

// Project requests / notifications

@Override
public CompletableFuture<Collection<ProjectInfo>> getProjects() {
return getLanguageClient().getProjects();
}

@Override
public void projectAdded(ProjectInfo project) {
projectRegistry.projectAdded(project);
}

@Override
public void projectRemoved(ProjectInfo project) {
projectRegistry.projectRemoved(project);
}

@Override
public CompletableFuture<ProjectInfo> getProjectInfo(QuteProjectParams params) {
return getLanguageClient().getProjectInfo(params);
}

// Other requests / notifications

@Override
public CompletableFuture<List<JavaTypeInfo>> getJavaTypes(QuteJavaTypesParams params) {
return getLanguageClient().getJavaTypes(params);
Expand All @@ -282,11 +299,6 @@ public CompletableFuture<ResolvedJavaTypeInfo> getResolvedJavaType(QuteResolvedJ
return getLanguageClient().getResolvedJavaType(params);
}

@Override
public CompletableFuture<ProjectInfo> getProjectInfo(QuteProjectParams params) {
return getLanguageClient().getProjectInfo(params);
}

@Override
public CompletableFuture<String> getJavadoc(QuteJavadocParams params) {
return getLanguageClient().getJavadoc(params);
Expand Down Expand Up @@ -355,7 +367,7 @@ public void notifyProgress(String progressId, WorkDoneProgressNotification notif
}

@Override
public void telemetryEvent (Object object) {
public void telemetryEvent(Object object) {
getLanguageClient().telemetryEvent(object);
}

Expand Down
Loading

0 comments on commit 1ebac0a

Please sign in to comment.