mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 16:28:56 +00:00
[eclipse/xtext#1569] Refactor Xtend to Java
Signed-off-by: Karsten Thoms <karsten.thoms@itemis.de>
This commit is contained in:
parent
07e48e0d94
commit
85db097d56
18 changed files with 379 additions and 690 deletions
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtext.util.UriUtil;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
public class FileProjectConfig implements IProjectConfig {
|
||||
private final URI path;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<FileSourceFolder> sourceFolders = new HashSet<>();
|
||||
|
||||
private final IWorkspaceConfig workspaceConfig;
|
||||
|
||||
public FileProjectConfig(URI path) {
|
||||
this(path, (IWorkspaceConfig) null);
|
||||
}
|
||||
|
||||
public FileProjectConfig(URI path, String name) {
|
||||
this(path, name, (IWorkspaceConfig) null);
|
||||
}
|
||||
|
||||
public FileProjectConfig(File root, String name) {
|
||||
this(root, name, (IWorkspaceConfig) null);
|
||||
}
|
||||
|
||||
public FileProjectConfig(File root) {
|
||||
this(root, (IWorkspaceConfig) null);
|
||||
}
|
||||
|
||||
public FileProjectConfig(URI path, IWorkspaceConfig workspaceConfig) {
|
||||
this(path, path.lastSegment(), workspaceConfig);
|
||||
}
|
||||
|
||||
public FileProjectConfig(URI path, String name, IWorkspaceConfig workspaceConfig) {
|
||||
this.path = UriUtil.toFolderURI(path);
|
||||
this.name = name;
|
||||
this.workspaceConfig = (workspaceConfig != null) ? workspaceConfig : new WorkspaceConfig(this);
|
||||
}
|
||||
|
||||
public FileProjectConfig(File root, String name, IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), name, workspaceConfig);
|
||||
}
|
||||
|
||||
public FileProjectConfig(File root, IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), root.getName(), workspaceConfig);
|
||||
}
|
||||
|
||||
public FileSourceFolder addSourceFolder(String relativePath) {
|
||||
FileSourceFolder sourceFolder = new FileSourceFolder(this, relativePath);
|
||||
sourceFolders.add(sourceFolder);
|
||||
return sourceFolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileSourceFolder findSourceFolderContaining(URI member) {
|
||||
return findFirst(sourceFolders, f -> f.contains(member));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof FileProjectConfig) {
|
||||
return Objects.equal(path, ((FileProjectConfig) obj).path);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return path.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Project " + name + " (" + path + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<FileSourceFolder> getSourceFolders() {
|
||||
return sourceFolders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IWorkspaceConfig getWorkspaceConfig() {
|
||||
return workspaceConfig;
|
||||
}
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.workspace
|
||||
|
||||
import java.io.File
|
||||
import java.util.Set
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
import org.eclipse.xtext.util.UriUtil
|
||||
|
||||
@Accessors
|
||||
class FileProjectConfig implements IProjectConfig {
|
||||
val URI path
|
||||
val String name
|
||||
val Set<FileSourceFolder> sourceFolders = newHashSet
|
||||
val IWorkspaceConfig workspaceConfig
|
||||
|
||||
new (URI path) {
|
||||
this(path, null as IWorkspaceConfig)
|
||||
}
|
||||
|
||||
new (URI path, String name) {
|
||||
this(path, name, null as IWorkspaceConfig)
|
||||
}
|
||||
|
||||
new (File root, String name) {
|
||||
this(root, name, null as IWorkspaceConfig)
|
||||
}
|
||||
|
||||
new (File root) {
|
||||
this(root, null as IWorkspaceConfig)
|
||||
}
|
||||
|
||||
new (URI path, IWorkspaceConfig workspaceConfig) {
|
||||
this(path, path.lastSegment, workspaceConfig)
|
||||
}
|
||||
|
||||
new (URI path, String name, IWorkspaceConfig workspaceConfig) {
|
||||
this.path = UriUtil.toFolderURI(path)
|
||||
this.name = name
|
||||
this.workspaceConfig = workspaceConfig ?: new WorkspaceConfig(this)
|
||||
}
|
||||
|
||||
new (File root, String name, IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), name, workspaceConfig)
|
||||
}
|
||||
|
||||
new (File root, IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), root.name, workspaceConfig)
|
||||
}
|
||||
|
||||
def FileSourceFolder addSourceFolder(String relativePath) {
|
||||
val sourceFolder = new FileSourceFolder(this, relativePath)
|
||||
sourceFolders += sourceFolder
|
||||
sourceFolder
|
||||
}
|
||||
|
||||
override FileSourceFolder findSourceFolderContaining(URI member) {
|
||||
sourceFolders.findFirst[sourceFolder|sourceFolder.contains(member)]
|
||||
}
|
||||
|
||||
override equals(Object obj) {
|
||||
if (obj instanceof FileProjectConfig) {
|
||||
return path == obj.path
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override hashCode() {
|
||||
path.hashCode
|
||||
}
|
||||
|
||||
override toString() {
|
||||
'''Project «name» («path»)'''
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@FinalFieldsConstructor
|
||||
class FileSourceFolder implements ISourceFolder {
|
||||
val FileProjectConfig parent
|
||||
val String name
|
||||
|
||||
override getName() {
|
||||
name
|
||||
}
|
||||
|
||||
override getPath() {
|
||||
val result = URI.createFileURI(name).resolve(parent.path)
|
||||
if (result.hasTrailingPathSeparator) {
|
||||
return result
|
||||
} else {
|
||||
return result.appendSegment("")
|
||||
}
|
||||
}
|
||||
|
||||
override equals(Object obj) {
|
||||
if (obj instanceof FileSourceFolder) {
|
||||
return path == obj.path
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override hashCode() {
|
||||
path.hashCode
|
||||
}
|
||||
|
||||
override toString() {
|
||||
'''«name» («path»)'''
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
public class FileSourceFolder implements ISourceFolder {
|
||||
private final FileProjectConfig parent;
|
||||
|
||||
private final String name;
|
||||
|
||||
public FileSourceFolder(FileProjectConfig parent, String name) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getPath() {
|
||||
final URI result = URI.createFileURI(name).resolve(parent.getPath());
|
||||
if (result.hasTrailingPathSeparator()) {
|
||||
return result;
|
||||
} else {
|
||||
return result.appendSegment("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (obj instanceof FileSourceFolder) {
|
||||
URI path1 = this.getPath();
|
||||
URI path2 = ((FileSourceFolder) obj).getPath();
|
||||
return Objects.equal(path1, path2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.getPath().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + " (" + getPath() + ")";
|
||||
}
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ import org.eclipse.emf.common.util.URI;
|
|||
|
||||
/**
|
||||
* A project is both a container for source folders as well as a physical location that outputs can be generated to.
|
||||
*
|
||||
*
|
||||
* In Eclipse, Maven and Gradle the concept is called a "Project". In IntelliJ it is called a "Module"
|
||||
*/
|
||||
public interface IProjectConfig {
|
||||
|
@ -32,7 +32,7 @@ public interface IProjectConfig {
|
|||
* Finds the source folder that physically contains this member or null if none was found.
|
||||
*/
|
||||
ISourceFolder findSourceFolderContaining(URI member);
|
||||
|
||||
|
||||
/**
|
||||
* @return the workspace config
|
||||
*/
|
||||
|
|
|
@ -12,12 +12,13 @@ import java.util.Set;
|
|||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
public interface IWorkspaceConfig {
|
||||
|
||||
|
||||
/**
|
||||
* @return the set of projects belonging to the current workspace. Note that these are usually only populated in the context of an IDE.
|
||||
* @return the set of projects belonging to the current workspace. Note that these are usually only populated in the
|
||||
* context of an IDE.
|
||||
*/
|
||||
Set<? extends IProjectConfig> getProjects();
|
||||
|
||||
|
||||
/**
|
||||
* @return the project whose source folders physically contain this member or null if none was found
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.common.notify.Adapter;
|
||||
import org.eclipse.emf.common.notify.Notifier;
|
||||
import org.eclipse.emf.common.notify.impl.AdapterImpl;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
|
||||
public class ProjectConfigAdapter {
|
||||
public static class ProjectConfigAdapterAdapter extends AdapterImpl {
|
||||
private final ProjectConfigAdapter element;
|
||||
|
||||
public ProjectConfigAdapterAdapter(ProjectConfigAdapter element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public ProjectConfigAdapter get() {
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdapterForType(Object object) {
|
||||
return object == ProjectConfigAdapter.class;
|
||||
}
|
||||
}
|
||||
|
||||
private final IProjectConfig projectConfig;
|
||||
|
||||
protected ProjectConfigAdapter(IProjectConfig projectConfig) {
|
||||
this.projectConfig = projectConfig;
|
||||
}
|
||||
|
||||
public static void install(ResourceSet resourceSet, IProjectConfig config) {
|
||||
new ProjectConfigAdapter(config).attachToEmfObject(resourceSet);
|
||||
}
|
||||
|
||||
public static ProjectConfigAdapter findInEmfObject(Notifier emfObject) {
|
||||
for (Adapter adapter : emfObject.eAdapters()) {
|
||||
if (adapter instanceof ProjectConfigAdapterAdapter) {
|
||||
return ((ProjectConfigAdapterAdapter) adapter).get();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ProjectConfigAdapter removeFromEmfObject(Notifier emfObject) {
|
||||
List<Adapter> adapters = emfObject.eAdapters();
|
||||
for (int i = 0, max = adapters.size(); i < max; i++) {
|
||||
Adapter adapter = adapters.get(i);
|
||||
if (adapter instanceof ProjectConfigAdapterAdapter) {
|
||||
emfObject.eAdapters().remove(i);
|
||||
return ((ProjectConfigAdapterAdapter) adapter).get();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void attachToEmfObject(Notifier emfObject) {
|
||||
ProjectConfigAdapter result = findInEmfObject(emfObject);
|
||||
if (result != null) {
|
||||
throw new IllegalStateException(
|
||||
"The given EMF object already contains an adapter for ProjectConfigAdapter");
|
||||
}
|
||||
ProjectConfigAdapterAdapter adapter = new ProjectConfigAdapterAdapter(this);
|
||||
emfObject.eAdapters().add(adapter);
|
||||
}
|
||||
|
||||
public IProjectConfig getProjectConfig() {
|
||||
return projectConfig;
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.workspace
|
||||
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtext.util.internal.EmfAdaptable
|
||||
|
||||
@EmfAdaptable
|
||||
@Accessors
|
||||
class ProjectConfigAdapter {
|
||||
val IProjectConfig projectConfig
|
||||
|
||||
protected new (IProjectConfig projectConfig) {
|
||||
this.projectConfig = projectConfig
|
||||
}
|
||||
|
||||
def static void install(ResourceSet resourceSet, IProjectConfig config) {
|
||||
new ProjectConfigAdapter(config).attachToEmfObject(resourceSet)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
|
||||
public class ProjectConfigProvider implements IProjectConfigProvider {
|
||||
@Override
|
||||
public IProjectConfig getProjectConfig(ResourceSet context) {
|
||||
ProjectConfigAdapter adapter = ProjectConfigAdapter.findInEmfObject(context);
|
||||
return adapter != null ? adapter.getProjectConfig() : null;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.workspace
|
||||
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
|
||||
class ProjectConfigProvider implements IProjectConfigProvider {
|
||||
|
||||
override getProjectConfig(ResourceSet context) {
|
||||
return ProjectConfigAdapter.findInEmfObject(context)?.projectConfig
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
public class UnknownProjectConfig implements IProjectConfig {
|
||||
private static final String NAME = "__unknown_project";
|
||||
|
||||
private final IWorkspaceConfig workspaceConfig;
|
||||
|
||||
public UnknownProjectConfig(IWorkspaceConfig workspaceConfig) {
|
||||
this.workspaceConfig = workspaceConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISourceFolder findSourceFolderContaining(URI member) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getPath() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends ISourceFolder> getSourceFolders() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IWorkspaceConfig getWorkspaceConfig() {
|
||||
return workspaceConfig;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
/**
|
||||
* The default workspace consists of multiple projects and always contains the unknown project that is used for
|
||||
* projectless files.
|
||||
*
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
public class WorkspaceConfig implements IWorkspaceConfig {
|
||||
private final UnknownProjectConfig unknown = new UnknownProjectConfig(this);
|
||||
|
||||
private final Map<String, IProjectConfig> projects = new LinkedHashMap<>();
|
||||
|
||||
public WorkspaceConfig(IProjectConfig... initialProjects) {
|
||||
addProject(unknown);
|
||||
for (IProjectConfig p : initialProjects) {
|
||||
addProject(p);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends IProjectConfig> getProjects() {
|
||||
return toSet(projects.values());
|
||||
}
|
||||
|
||||
public void addProject(IProjectConfig project) {
|
||||
projects.put(project.getName(), project);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IProjectConfig findProjectByName(String name) {
|
||||
return projects.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IProjectConfig findProjectContaining(URI member) {
|
||||
return projects.values().stream()
|
||||
.filter(projectCfg -> projectCfg.findSourceFolderContaining(member) != null)
|
||||
.max((cfg1, cfg2) -> cfg1.getPath().segmentCount() - cfg2.getPath().segmentCount())
|
||||
.orElse(unknown);
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.workspace
|
||||
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
|
||||
/**
|
||||
* The default workspace consists of multiple projects and always contains the unknown project that is used for projectless files.
|
||||
*
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
class WorkspaceConfig implements IWorkspaceConfig {
|
||||
|
||||
val unknown = new UnknownProjectConfig(this)
|
||||
val projects = <String, IProjectConfig>newHashMap
|
||||
|
||||
new(IProjectConfig... initialProjects) {
|
||||
addProject(unknown)
|
||||
initialProjects.forEach[addProject]
|
||||
}
|
||||
|
||||
override getProjects() {
|
||||
projects.values.toSet
|
||||
}
|
||||
|
||||
def void addProject(IProjectConfig project) {
|
||||
projects.put(project.name, project)
|
||||
}
|
||||
|
||||
override findProjectByName(String name) {
|
||||
projects.get(name)
|
||||
}
|
||||
|
||||
override findProjectContaining(URI member) {
|
||||
val candidates = projects.values.filter [
|
||||
findSourceFolderContaining(member) !== null
|
||||
]
|
||||
if (!candidates.empty)
|
||||
return candidates.maxBy[path.segmentCount]
|
||||
else
|
||||
return unknown
|
||||
}
|
||||
}
|
||||
|
||||
@Accessors
|
||||
@FinalFieldsConstructor
|
||||
class UnknownProjectConfig implements IProjectConfig {
|
||||
|
||||
val String name = '__unknown_project'
|
||||
val IWorkspaceConfig workspaceConfig
|
||||
|
||||
override findSourceFolderContaining(URI member) {
|
||||
null
|
||||
}
|
||||
|
||||
override getPath() {
|
||||
null
|
||||
}
|
||||
|
||||
override getSourceFolders() {
|
||||
emptySet
|
||||
}
|
||||
|
||||
}
|
|
@ -1,139 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.util.UriUtil;
|
||||
import org.eclipse.xtext.workspace.FileSourceFolder;
|
||||
import org.eclipse.xtext.workspace.IProjectConfig;
|
||||
import org.eclipse.xtext.workspace.IWorkspaceConfig;
|
||||
import org.eclipse.xtext.workspace.WorkspaceConfig;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
|
||||
@Accessors
|
||||
@SuppressWarnings("all")
|
||||
public class FileProjectConfig implements IProjectConfig {
|
||||
private final URI path;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<FileSourceFolder> sourceFolders = CollectionLiterals.<FileSourceFolder>newHashSet();
|
||||
|
||||
private final IWorkspaceConfig workspaceConfig;
|
||||
|
||||
public FileProjectConfig(final URI path) {
|
||||
this(path, ((IWorkspaceConfig) null));
|
||||
}
|
||||
|
||||
public FileProjectConfig(final URI path, final String name) {
|
||||
this(path, name, ((IWorkspaceConfig) null));
|
||||
}
|
||||
|
||||
public FileProjectConfig(final File root, final String name) {
|
||||
this(root, name, ((IWorkspaceConfig) null));
|
||||
}
|
||||
|
||||
public FileProjectConfig(final File root) {
|
||||
this(root, ((IWorkspaceConfig) null));
|
||||
}
|
||||
|
||||
public FileProjectConfig(final URI path, final IWorkspaceConfig workspaceConfig) {
|
||||
this(path, path.lastSegment(), workspaceConfig);
|
||||
}
|
||||
|
||||
public FileProjectConfig(final URI path, final String name, final IWorkspaceConfig workspaceConfig) {
|
||||
this.path = UriUtil.toFolderURI(path);
|
||||
this.name = name;
|
||||
IWorkspaceConfig _elvis = null;
|
||||
if (workspaceConfig != null) {
|
||||
_elvis = workspaceConfig;
|
||||
} else {
|
||||
WorkspaceConfig _workspaceConfig = new WorkspaceConfig(this);
|
||||
_elvis = _workspaceConfig;
|
||||
}
|
||||
this.workspaceConfig = _elvis;
|
||||
}
|
||||
|
||||
public FileProjectConfig(final File root, final String name, final IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), name, workspaceConfig);
|
||||
}
|
||||
|
||||
public FileProjectConfig(final File root, final IWorkspaceConfig workspaceConfig) {
|
||||
this(UriUtil.createFolderURI(root), root.getName(), workspaceConfig);
|
||||
}
|
||||
|
||||
public FileSourceFolder addSourceFolder(final String relativePath) {
|
||||
FileSourceFolder _xblockexpression = null;
|
||||
{
|
||||
final FileSourceFolder sourceFolder = new FileSourceFolder(this, relativePath);
|
||||
this.sourceFolders.add(sourceFolder);
|
||||
_xblockexpression = sourceFolder;
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileSourceFolder findSourceFolderContaining(final URI member) {
|
||||
final Function1<FileSourceFolder, Boolean> _function = (FileSourceFolder sourceFolder) -> {
|
||||
return Boolean.valueOf(sourceFolder.contains(member));
|
||||
};
|
||||
return IterableExtensions.<FileSourceFolder>findFirst(this.sourceFolders, _function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if ((obj instanceof FileProjectConfig)) {
|
||||
return Objects.equal(this.path, ((FileProjectConfig)obj).path);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.path.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("Project ");
|
||||
_builder.append(this.name);
|
||||
_builder.append(" (");
|
||||
_builder.append(this.path);
|
||||
_builder.append(")");
|
||||
return _builder.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public URI getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public Set<FileSourceFolder> getSourceFolders() {
|
||||
return this.sourceFolders;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IWorkspaceConfig getWorkspaceConfig() {
|
||||
return this.workspaceConfig;
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.workspace.FileProjectConfig;
|
||||
import org.eclipse.xtext.workspace.ISourceFolder;
|
||||
|
||||
@FinalFieldsConstructor
|
||||
@SuppressWarnings("all")
|
||||
public class FileSourceFolder implements ISourceFolder {
|
||||
private final FileProjectConfig parent;
|
||||
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getPath() {
|
||||
final URI result = URI.createFileURI(this.name).resolve(this.parent.getPath());
|
||||
boolean _hasTrailingPathSeparator = result.hasTrailingPathSeparator();
|
||||
if (_hasTrailingPathSeparator) {
|
||||
return result;
|
||||
} else {
|
||||
return result.appendSegment("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if ((obj instanceof FileSourceFolder)) {
|
||||
URI _path = this.getPath();
|
||||
URI _path_1 = ((FileSourceFolder)obj).getPath();
|
||||
return Objects.equal(_path, _path_1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.getPath().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append(this.name);
|
||||
_builder.append(" (");
|
||||
URI _path = this.getPath();
|
||||
_builder.append(_path);
|
||||
_builder.append(")");
|
||||
return _builder.toString();
|
||||
}
|
||||
|
||||
public FileSourceFolder(final FileProjectConfig parent, final String name) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import java.util.List;
|
||||
import org.eclipse.emf.common.notify.Adapter;
|
||||
import org.eclipse.emf.common.notify.Notifier;
|
||||
import org.eclipse.emf.common.notify.impl.AdapterImpl;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtext.util.internal.EmfAdaptable;
|
||||
import org.eclipse.xtext.workspace.IProjectConfig;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
|
||||
@EmfAdaptable
|
||||
@Accessors
|
||||
@SuppressWarnings("all")
|
||||
public class ProjectConfigAdapter {
|
||||
public static class ProjectConfigAdapterAdapter extends AdapterImpl {
|
||||
private ProjectConfigAdapter element;
|
||||
|
||||
public ProjectConfigAdapterAdapter(final ProjectConfigAdapter element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public ProjectConfigAdapter get() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdapterForType(final Object object) {
|
||||
return object == ProjectConfigAdapter.class;
|
||||
}
|
||||
}
|
||||
|
||||
private final IProjectConfig projectConfig;
|
||||
|
||||
protected ProjectConfigAdapter(final IProjectConfig projectConfig) {
|
||||
this.projectConfig = projectConfig;
|
||||
}
|
||||
|
||||
public static void install(final ResourceSet resourceSet, final IProjectConfig config) {
|
||||
new ProjectConfigAdapter(config).attachToEmfObject(resourceSet);
|
||||
}
|
||||
|
||||
public static ProjectConfigAdapter findInEmfObject(final Notifier emfObject) {
|
||||
for (Adapter adapter : emfObject.eAdapters()) {
|
||||
if (adapter instanceof ProjectConfigAdapter.ProjectConfigAdapterAdapter) {
|
||||
return ((ProjectConfigAdapter.ProjectConfigAdapterAdapter) adapter).get();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ProjectConfigAdapter removeFromEmfObject(final Notifier emfObject) {
|
||||
List<Adapter> adapters = emfObject.eAdapters();
|
||||
for(int i = 0, max = adapters.size(); i < max; i++) {
|
||||
Adapter adapter = adapters.get(i);
|
||||
if (adapter instanceof ProjectConfigAdapter.ProjectConfigAdapterAdapter) {
|
||||
emfObject.eAdapters().remove(i);
|
||||
return ((ProjectConfigAdapter.ProjectConfigAdapterAdapter) adapter).get();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void attachToEmfObject(final Notifier emfObject) {
|
||||
ProjectConfigAdapter result = findInEmfObject(emfObject);
|
||||
if (result != null)
|
||||
throw new IllegalStateException("The given EMF object already contains an adapter for ProjectConfigAdapter");
|
||||
ProjectConfigAdapter.ProjectConfigAdapterAdapter adapter = new ProjectConfigAdapter.ProjectConfigAdapterAdapter(this);
|
||||
emfObject.eAdapters().add(adapter);
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IProjectConfig getProjectConfig() {
|
||||
return this.projectConfig;
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtext.workspace.IProjectConfig;
|
||||
import org.eclipse.xtext.workspace.IProjectConfigProvider;
|
||||
import org.eclipse.xtext.workspace.ProjectConfigAdapter;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class ProjectConfigProvider implements IProjectConfigProvider {
|
||||
@Override
|
||||
public IProjectConfig getProjectConfig(final ResourceSet context) {
|
||||
ProjectConfigAdapter _findInEmfObject = ProjectConfigAdapter.findInEmfObject(context);
|
||||
IProjectConfig _projectConfig = null;
|
||||
if (_findInEmfObject!=null) {
|
||||
_projectConfig=_findInEmfObject.getProjectConfig();
|
||||
}
|
||||
return _projectConfig;
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import java.util.Set;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
|
||||
import org.eclipse.xtext.workspace.IProjectConfig;
|
||||
import org.eclipse.xtext.workspace.ISourceFolder;
|
||||
import org.eclipse.xtext.workspace.IWorkspaceConfig;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
|
||||
@Accessors
|
||||
@FinalFieldsConstructor
|
||||
@SuppressWarnings("all")
|
||||
public class UnknownProjectConfig implements IProjectConfig {
|
||||
private final String name = "__unknown_project";
|
||||
|
||||
private final IWorkspaceConfig workspaceConfig;
|
||||
|
||||
@Override
|
||||
public ISourceFolder findSourceFolderContaining(final URI member) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getPath() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends ISourceFolder> getSourceFolders() {
|
||||
return CollectionLiterals.<ISourceFolder>emptySet();
|
||||
}
|
||||
|
||||
public UnknownProjectConfig(final IWorkspaceConfig workspaceConfig) {
|
||||
super();
|
||||
this.workspaceConfig = workspaceConfig;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IWorkspaceConfig getWorkspaceConfig() {
|
||||
return this.workspaceConfig;
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.workspace;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtext.workspace.IProjectConfig;
|
||||
import org.eclipse.xtext.workspace.ISourceFolder;
|
||||
import org.eclipse.xtext.workspace.IWorkspaceConfig;
|
||||
import org.eclipse.xtext.workspace.UnknownProjectConfig;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Conversions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
|
||||
/**
|
||||
* The default workspace consists of multiple projects and always contains the unknown project that is used for projectless files.
|
||||
*
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class WorkspaceConfig implements IWorkspaceConfig {
|
||||
private final UnknownProjectConfig unknown = new UnknownProjectConfig(this);
|
||||
|
||||
private final HashMap<String, IProjectConfig> projects = CollectionLiterals.<String, IProjectConfig>newHashMap();
|
||||
|
||||
public WorkspaceConfig(final IProjectConfig... initialProjects) {
|
||||
this.addProject(this.unknown);
|
||||
final Consumer<IProjectConfig> _function = (IProjectConfig it) -> {
|
||||
this.addProject(it);
|
||||
};
|
||||
((List<IProjectConfig>)Conversions.doWrapArray(initialProjects)).forEach(_function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends IProjectConfig> getProjects() {
|
||||
return IterableExtensions.<IProjectConfig>toSet(this.projects.values());
|
||||
}
|
||||
|
||||
public void addProject(final IProjectConfig project) {
|
||||
this.projects.put(project.getName(), project);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IProjectConfig findProjectByName(final String name) {
|
||||
return this.projects.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IProjectConfig findProjectContaining(final URI member) {
|
||||
final Function1<IProjectConfig, Boolean> _function = (IProjectConfig it) -> {
|
||||
ISourceFolder _findSourceFolderContaining = it.findSourceFolderContaining(member);
|
||||
return Boolean.valueOf((_findSourceFolderContaining != null));
|
||||
};
|
||||
final Iterable<IProjectConfig> candidates = IterableExtensions.<IProjectConfig>filter(this.projects.values(), _function);
|
||||
boolean _isEmpty = IterableExtensions.isEmpty(candidates);
|
||||
boolean _not = (!_isEmpty);
|
||||
if (_not) {
|
||||
final Function1<IProjectConfig, Integer> _function_1 = (IProjectConfig it) -> {
|
||||
return Integer.valueOf(it.getPath().segmentCount());
|
||||
};
|
||||
return IterableExtensions.<IProjectConfig, Integer>maxBy(candidates, _function_1);
|
||||
} else {
|
||||
return this.unknown;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue