Skip to content
Snippets Groups Projects
Commit ca28dbf3 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Added folder handling to Tpd import


Signed-off-by: default avatarMiklos Magyari <miklos.magyari@sigmatechnology.com>
parent 0e02ec83
No related branches found
No related tags found
1 merge request!65Added folder handling to Tpd import
......@@ -7,14 +7,21 @@
******************************************************************************/
package org.eclipse.titan.lsp;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.titan.lsp.AST.Module;
import org.eclipse.titan.lsp.AST.TTCN3.definitions.ICommentable;
import org.eclipse.titan.lsp.AST.TTCN3.definitions.TTCN3Module;
import org.eclipse.titan.lsp.common.product.TpdImporter;
import org.eclipse.titan.lsp.core.Project;
import org.eclipse.titan.lsp.core.ProjectItem;
import org.eclipse.titan.lsp.declarationsearch.Declaration;
......@@ -22,6 +29,8 @@ import org.eclipse.titan.lsp.declarationsearch.IdentifierFinderVisitor;
import org.eclipse.titan.lsp.hover.HoverContentType;
import org.eclipse.titan.lsp.lspextensions.DocumentCommentParams;
import org.eclipse.titan.lsp.lspextensions.ExcludedStatesChangedParams;
import org.eclipse.titan.lsp.lspextensions.OpenTpdParams;
import org.eclipse.titan.lsp.lspextensions.TpdProjectFiles;
public class TitanLspExtensionsService implements LspExtensionsService {
private final TitanLanguageServer languageServer;
......@@ -84,4 +93,34 @@ public class TitanLspExtensionsService implements LspExtensionsService {
}
}
@Override
public CompletableFuture<TpdProjectFiles> openTpd(OpenTpdParams params) {
return CompletableFuture.supplyAsync(() -> {
Path path = null;
try {
final String filename = URLDecoder.decode(params.getTpdPath());
path = Path.of(filename);
} catch (Exception e) {
e.printStackTrace();
}
TpdImporter importer = new TpdImporter(path.toFile());
importer.processTpd(false, false, false, null);
final Project project = Project.INSTANCE;
project.setTpdFiles(importer.getFileNames());
TitanLanguageServer.analyzeProject(project);
final TpdProjectFiles tpdFiles = new TpdProjectFiles();
final List<String> fileList = new ArrayList<>();
importer.getFileNames().stream().forEach(filepath -> {
fileList.add(filepath.toString());
});
tpdFiles.success = true;
tpdFiles.result = Either.forLeft(fileList);
return tpdFiles;
});
}
}
......@@ -353,8 +353,18 @@ public class TpdImporter {
final URI mainProjectFileFolderURI = mainProjectFileFolderPath.toUri();
final List<String> projectsCreated = new ArrayList<>();
if (projectsToImport.get(resolvedProjectFileURI) != null) {
final List<Node> packedProjects = loadPackedProjects(projectsToImport.get(resolvedProjectFileURI));
final File a = new File(resolvedProjectFileURI);
File absolute = null;
try {
absolute = a.getCanonicalFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if (projectsToImport.get(absolute.toURI()) != null) {
final List<Node> packedProjects = loadPackedProjects(projectsToImport.get(absolute.toURI()));
for (final Node node : packedProjects) {
// final IProject project = createProject(node, false);
// if (project == null) {
......@@ -366,11 +376,18 @@ public class TpdImporter {
}
projectsToImport.entrySet().stream().forEach((x) -> {
final NodeList referencedProjectsList = x.getValue().getDocumentElement().getChildNodes();
final Node node = getNodebyName(referencedProjectsList, ProjectFormatConstants.FILES_NODE);
loadFilesData(node, projectFile, mainProjectFileFolderURI);
loadFileList(node, projectFile, x.getKey());
final Node folderNode = getNodebyName(referencedProjectsList, ProjectFormatConstants.FOLDERS_NODE);
if (folderNode != null) {
loadFoldersData(folderNode, mainProjectFileFolderURI);
loadFileList(folderNode, projectFile, x.getKey());
}
final Node node = getNodebyName(referencedProjectsList, ProjectFormatConstants.FILES_NODE);
if (node != null) {
loadFilesData(node, projectFile, mainProjectFileFolderURI);
loadFileList(node, projectFile, x.getKey());
}
});
return islimitImportProcesses;
......@@ -410,7 +427,17 @@ public class TpdImporter {
* @return true if there were no errors, false otherwise.
* */
private boolean loadURIDocuments(final URI file, final Validator validator) {
if (projectsToImport.containsKey(file)) {
final File a = new File(file);
final Document document = getDocumentFromFile(file.getPath());
File absolute = null;
try {
absolute = a.getCanonicalFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if (projectsToImport.containsKey(absolute.toURI())) {
return true;
}
......@@ -420,7 +447,6 @@ public class TpdImporter {
return false;
}
final Document document = getDocumentFromFile(file.getPath());
if (document == null) {
final StringBuilder builder = new StringBuilder("It was not possible to load the imported project file: '" + file.toString()
+ "'\n");
......@@ -444,17 +470,8 @@ public class TpdImporter {
// ProjectFileHandler.clearNode(document);
final File a = new File(file);
File absolute;
try {
absolute = a.getCanonicalFile();
projectsToImport.put(absolute.toURI(), document);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
projectsToImport.put(absolute.toURI(), document);
final Element mainElement = document.getDocumentElement();
final NodeList mainNodes = mainElement.getChildNodes();
final Node referencedProjectsNode = getNodebyName(mainNodes, ProjectFormatConstants.REFERENCED_PROJECTS_NODE);
......@@ -583,6 +600,93 @@ public class TpdImporter {
return result;
}
/**
* Load the information describing folders.
*
* @param foldersNode
* the node to load from.
* @param project
* the project to set this information on.
* @param projectFileFolderURI
* the location of the project file's folder.
*
* @return true if the import was successful, false otherwise.
* */
private boolean loadFoldersData(final Node foldersNode, final URI projectFileFolderURI) {
final NodeList folderNodeList = foldersNode.getChildNodes();
for (int i = 0, size = folderNodeList.getLength(); i < size; i++) {
final Node folderItem = folderNodeList.item(i);
if (folderItem.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final NamedNodeMap attributeMap = folderItem.getAttributes();
if (attributeMap == null) {
continue;
}
final Node projectRelativePathNode = attributeMap.getNamedItem(ProjectFormatConstants.FOLDER_ECLIPSE_LOCATION_NODE);
if (projectRelativePathNode == null) {
// displayError("Import failed", "Error while importing project " + project.getName()
// + " the project relative path attribute of the " + i + " th folder is missing");
return false;
}
final String projectRelativePath = projectRelativePathNode.getTextContent();
final Node relativeURINode = attributeMap.getNamedItem(ProjectFormatConstants.FOLDER_RELATIVE_LOCATION);
final Node rawURINode = attributeMap.getNamedItem(ProjectFormatConstants.FOLDER_RAW_LOCATION);
// final IFolder folder = project.getFolder(projectRelativePath);
// if (!folder.exists()) {
// try {
// if (relativeURINode != null) {
// final String relativeLocation = relativeURINode.getTextContent();
// //if relativeLocation == "virtual:/virtual" then
// //create a workaround according to the rawURI branch
// if( "virtual:/virtual".equals(relativeLocation) ) {
// folder.createLink(URI.create(relativeLocation), IResource.ALLOW_MISSING_LOCAL, null);
// continue;
// }
//
// final URI absoluteURI = TITANPathUtilities.resolvePathURI(relativeLocation, URIUtil.toPath(projectFileFolderURI).toOSString());
// if (absoluteURI == null) {
// // The URI cannot be resolved - for example it
// // contains not existing environment variables
// continue;
// }
// if (TitanURIUtil.isPrefix(projectLocationURI, absoluteURI)) {
// folder.create(false, true, null);
// } else {
// final File tmpFolder = new File(absoluteURI);
// if (tmpFolder.exists()) {
// folder.createLink(absoluteURI, IResource.ALLOW_MISSING_LOCAL, null);
// } else {
// ErrorReporter.logError("Error while importing folders into project `" + project.getName() + "'. Folder `"
// + absoluteURI + "' does not exist");
// continue;
// }
// }
// } else if (rawURINode != null) {
// final String rawLocation = rawURINode.getTextContent();
// folder.createLink(URI.create(rawLocation), IResource.ALLOW_MISSING_LOCAL, null);
// } else {
// TITANDebugConsole.println("Cannot create the resource " + folder.getFullPath().toString()
// + " the location information is missing or corrupted");
// }
// } catch (CoreException e) {
// ErrorReporter.logError("Error while importing folders into project: " + e );
// //be silent, it can happen normally
// }
// } else {
// ErrorReporter.logWarning("Folder to be imported `" + folder.getName() + "' already exists in project `" + project.getName()
// + "'");
// }
}
return true;
}
/**
* Load the information describing files.
*
......
......@@ -155,9 +155,24 @@ public class Project {
filelist.stream().forEach((toBeIncluded) -> {
final ProjectItem existing = getProjectItem(toBeIncluded);
if (existing != null) {
existing.setExcluded(false);
System.out.println("Including file: " + existing.getPath().toString());
if (existing.isDirectory()) {
includeFolder(existing.getPath());
existing.setExcluded(false);
} else {
existing.setExcluded(false);
}
System.out.println("Including :: " + existing.getPath().toString());
}
});
}
private void includeFolder(final Path path) {
final String folder = path.toString();
projectItems.entrySet().stream()
.forEach(item -> {
if (item.getKey().toString().startsWith(folder)) {
item.getValue().setExcluded(false);
}
});
}
}
......@@ -54,6 +54,7 @@ public class WorkspaceVisitor implements FileVisitor<Path> {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
files.add(dir);
return FileVisitResult.CONTINUE;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment