Skip to content

Commit

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

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 19, 2023
1 parent e944bd1 commit 12396c9
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 62 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 @@ -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 @@ -108,8 +108,6 @@ 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);
Expand All @@ -135,7 +133,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 +160,18 @@ 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()) {
for (ProjectInfo projectInfo : projects) {
// 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(projectInfo, progressSupport);
}
}
});
}

/**
Expand Down Expand Up @@ -267,6 +263,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 +302,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 @@ -359,4 +374,5 @@ public void telemetryEvent (Object object) {
getLanguageClient().telemetryEvent(object);
}


}
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
import org.eclipse.lsp4j.WorkDoneProgressEnd;
import org.eclipse.lsp4j.WorkDoneProgressReport;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.redhat.qute.commons.JavaTypeInfo;
Expand Down Expand Up @@ -407,58 +406,38 @@ public CompletableFuture<ProjectInfo> getProjectInfo(QuteProjectParams params) {
* @param workspaceFolder the workspace folder.
* @param progressSupport the LSP client progress support and null otherwise.
*/
public void tryToLoadQuteProject(WorkspaceFolder workspaceFolder, ProgressSupport progressSupport) {
String projectName = workspaceFolder.getName();

public void tryToLoadQuteProject(ProjectInfo projectInfo, ProgressSupport progressSupport) {
String projectName = projectInfo.getUri();
String progressId = createAndStartProgress(projectName, progressSupport);

// Load Qute project from the Java component (collect Java data model)
String projectUri = workspaceFolder.getUri();
QuteProjectParams params = new QuteProjectParams(projectUri);
getProjectInfo(params)
.thenAccept(projectInfo -> {
if (projectInfo == null) {
// The workspace folder is not a Qute project, end the process
endProgress(progressId, progressSupport);
return;
}

// The workspace folder is a Qute project, load the data model from Java
// component
QuteProject project = getProject(projectInfo, false);
QuteProject project = getProject(projectInfo, false);
if (progressSupport != null) {
WorkDoneProgressReport report = new WorkDoneProgressReport();
report.setMessage("Loading data model for '" + projectName + "' Qute project.");
report.setPercentage(10);
progressSupport.notifyProgress(progressId, report);
}
project.getDataModelProject()
.thenAccept(dataModel -> {
// The Java data model is collected for the project, validate all templates of
// the project
if (progressSupport != null) {
WorkDoneProgressReport report = new WorkDoneProgressReport();
report.setMessage("Loading data model for '" + projectName + "' Qute project.");
report.setPercentage(10);
report.setMessage(
"Validating Qute templates for '" + projectName + "' Qute project.");
report.setPercentage(80);
progressSupport.notifyProgress(progressId, report);
}
project.getDataModelProject()
.thenAccept(dataModel -> {
// The Java data model is collected for the project, validate all templates of
// the project
if (progressSupport != null) {
WorkDoneProgressReport report = new WorkDoneProgressReport();
report.setMessage(
"Validating Qute templates for '" + projectName + "' Qute project.");
report.setPercentage(80);
progressSupport.notifyProgress(progressId, report);
}
// Validate Qute templates
project.validateClosedTemplates();

// End progress
endProgress(progressId, progressSupport);
// Validate Qute templates
project.validateClosedTemplates();

}).exceptionally((a) -> {
endProgress(progressId, progressSupport);
return null;
});
// End progress
endProgress(progressId, progressSupport);

}).exceptionally((a) -> {
endProgress(progressId, progressSupport);
return null;
});

}

private static String createAndStartProgress(String projectName, ProgressSupport progressSupport) {
Expand All @@ -485,4 +464,14 @@ private static void endProgress(String progressId, ProgressSupport progressSuppo
progressSupport.notifyProgress(progressId, end);
}
}

public void projectAdded(ProjectInfo project) {
// TODO Auto-generated method stub

}

public void projectRemoved(ProjectInfo project) {
// TODO Auto-generated method stub

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package com.redhat.qute.project;

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

Expand Down Expand Up @@ -167,4 +168,19 @@ public CompletableFuture<WorkspaceEdit> generateMissingJavaMember(GenerateMissin
public CompletableFuture<String> getJavadoc(QuteJavadocParams params) {
return CompletableFuture.completedFuture(null);
}

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

@Override
public void projectAdded(ProjectInfo project) {

}

@Override
public void projectRemoved(ProjectInfo project) {

}
}

0 comments on commit 12396c9

Please sign in to comment.