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 5719ac0
Show file tree
Hide file tree
Showing 28 changed files with 574 additions and 113 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,18 @@ public static QuteSupportForTemplate getInstance() {
return INSTANCE;
}

public List<ProjectInfo> getProjects(IJDTUtils utils, IProgressMonitor monitor) {
List<ProjectInfo> quteProjects = new ArrayList<>();
IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : allProjects) {
IJavaProject javaProject = getJavaProject(project);
if (isQuteProject(javaProject)) {
quteProjects.add(JDTQuteProjectUtils.getProjectInfo(javaProject));
}
}
return quteProjects;
}

/**
* Returns the project information for the given project Uri.
*
Expand Down Expand Up @@ -405,12 +418,24 @@ 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;
}
return JDTTypeUtils.findType(javaProject, QuteJavaConstants.TEMPLATE_CLASS) != null;
}


/**
* Returns the Java project of the given project and null otherwise.
*
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,16 @@
*******************************************************************************/
package com.redhat.qute.jdt.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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 @@ -40,7 +45,18 @@ 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
e.printStackTrace();
}
return new ProjectInfo(projectUri, projectDependencies, templateBaseDir);
}

/**
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()) {
// Load qute projects
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
*******************************************************************************/
package com.redhat.qute.ls.api;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;

import com.redhat.qute.commons.ProjectInfo;
Expand All @@ -26,6 +28,38 @@
*/
public interface QuteProjectInfoProvider {

/**
* Returns all Qute projects from the workspace.
*
* @return all Qute projects from the workspace.
*/
@JsonRequest("qute/template/projects")
CompletableFuture<Collection<ProjectInfo>> getProjects();

/**
* Notification received when a Qute project is added in the workspace.
*
* @param project the Qute project which is added in the workspace.
*/
@JsonNotification("qute/template/project/added")
void projectAdded(ProjectInfo project);

/**
* Notification received when a Qute project is closed / removed from the
* workspace.
*
* @param project the Qute project which is closed / removed from the workspace.
*/
@JsonNotification("qute/template/project/removed")
void projectRemoved(ProjectInfo project);

/**
* Returns the Qute project from the given template file uri parameter.
*
* @param params the template file uri parameter.
* @return
*/
@JsonRequest("qute/template/project")
CompletableFuture<ProjectInfo> getProjectInfo(QuteProjectParams params);

}
Loading

0 comments on commit 5719ac0

Please sign in to comment.