mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
[eclipse/xtext#1679] ported xtend code to java
Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
This commit is contained in:
parent
54fa97408e
commit
9a055fd969
57 changed files with 1249 additions and 2437 deletions
|
@ -1,11 +1,11 @@
|
|||
/**
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.tests.server;
|
||||
|
||||
import org.eclipse.xtext.testing.AbstractLanguageServerTest;
|
||||
|
@ -13,9 +13,10 @@ import org.eclipse.xtext.testing.AbstractLanguageServerTest;
|
|||
/**
|
||||
* @author akosyakov - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public abstract class AbstractTestLangLanguageServerTest extends AbstractLanguageServerTest {
|
||||
public AbstractTestLangLanguageServerTest() {
|
||||
super("testlang");
|
||||
}
|
||||
|
||||
public AbstractTestLangLanguageServerTest() {
|
||||
super("testlang");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.tests.server
|
||||
|
||||
import org.eclipse.xtext.testing.AbstractLanguageServerTest
|
||||
|
||||
/**
|
||||
* @author akosyakov - Initial contribution and API
|
||||
*/
|
||||
abstract class AbstractTestLangLanguageServerTest extends AbstractLanguageServerTest {
|
||||
|
||||
new() {
|
||||
super("testlang")
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.eclipse.xtext.util.DisposableRegistry;
|
||||
import org.eclipse.xtext.util.IDisposable;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Provider for executor services. By calling {@link #dispose()} all created executor services are shut down.
|
||||
* <p>
|
||||
* In some situations it is necessary to use multiple instances of executor services in order to avoid deadlocks. That
|
||||
* can be achieved with the {@link #get(String)} method, which will return a different instance for each key.
|
||||
*/
|
||||
@Singleton
|
||||
public class ExecutorServiceProvider implements Provider<ExecutorService>, IDisposable {
|
||||
@Inject
|
||||
public void registerTo(DisposableRegistry disposableRegistry) {
|
||||
disposableRegistry.register(this);
|
||||
}
|
||||
|
||||
private final Map<String, ExecutorService> instanceCache = Maps.newHashMapWithExpectedSize(3);
|
||||
|
||||
@Override
|
||||
public ExecutorService get() {
|
||||
return get(null);
|
||||
}
|
||||
|
||||
public ExecutorService get(String key) {
|
||||
ExecutorService result = instanceCache.get(key);
|
||||
if (result == null) {
|
||||
synchronized (instanceCache) {
|
||||
result = instanceCache.get(key);
|
||||
if (result == null) {
|
||||
result = createInstance(key);
|
||||
instanceCache.put(key, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected ExecutorService createInstance(String key) {
|
||||
return Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
for (ExecutorService executorService : instanceCache.values()) {
|
||||
executorService.shutdown();
|
||||
}
|
||||
instanceCache.clear();
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide
|
||||
|
||||
import com.google.common.collect.Maps
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Provider
|
||||
import com.google.inject.Singleton
|
||||
import java.util.Map
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import org.eclipse.xtext.util.DisposableRegistry
|
||||
import org.eclipse.xtext.util.IDisposable
|
||||
|
||||
/**
|
||||
* Provider for executor services. By calling {@link #dispose()} all created executor services are shut down.
|
||||
* <p>
|
||||
* In some situations it is necessary to use multiple instances of executor services in order to avoid deadlocks.
|
||||
* That can be achieved with the {@link #get(String)} method, which will return a different instance for each key.
|
||||
*/
|
||||
@Singleton
|
||||
class ExecutorServiceProvider implements Provider<ExecutorService>, IDisposable {
|
||||
|
||||
@Inject
|
||||
def registerTo(DisposableRegistry disposableRegistry) {
|
||||
disposableRegistry.register(this)
|
||||
}
|
||||
|
||||
val Map<String, ExecutorService> instanceCache = Maps.newHashMapWithExpectedSize(3)
|
||||
|
||||
override get() {
|
||||
get(null)
|
||||
}
|
||||
|
||||
def ExecutorService get(String key) {
|
||||
var result = instanceCache.get(key)
|
||||
if (result === null) {
|
||||
synchronized (instanceCache) {
|
||||
result = instanceCache.get(key)
|
||||
if (result === null) {
|
||||
result = createInstance(key)
|
||||
instanceCache.put(key, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
protected def ExecutorService createInstance(String key) {
|
||||
Executors.newCachedThreadPool
|
||||
}
|
||||
|
||||
override dispose() {
|
||||
for (executorService : instanceCache.values) {
|
||||
executorService.shutdown()
|
||||
}
|
||||
instanceCache.clear()
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
public class AlternativeImageDescription implements IImageDescription {
|
||||
private final List<String> imageIDs;
|
||||
|
||||
public AlternativeImageDescription(Iterable<String> imageIDs) {
|
||||
this.imageIDs = ImmutableList.copyOf(imageIDs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((imageIDs == null) ? 0 : imageIDs.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AlternativeImageDescription other = (AlternativeImageDescription) obj;
|
||||
if (imageIDs == null) {
|
||||
if (other.imageIDs != null)
|
||||
return false;
|
||||
} else if (!imageIDs.equals(other.imageIDs))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("imageIDs", imageIDs);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public List<String> getImageIDs() {
|
||||
return imageIDs;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class DecoratedImageDescription implements IImageDescription {
|
||||
private final IImageDescription baseImage;
|
||||
|
||||
private final List<IImageDescription> decorators;
|
||||
|
||||
public DecoratedImageDescription(IImageDescription baseImage, IImageDescription... decorators) {
|
||||
this.baseImage = baseImage;
|
||||
this.decorators = ImmutableList.<IImageDescription>copyOf(decorators);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((baseImage == null) ? 0 : baseImage.hashCode());
|
||||
result = prime * result + ((decorators == null) ? 0 : decorators.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
DecoratedImageDescription other = (DecoratedImageDescription) obj;
|
||||
if (baseImage == null) {
|
||||
if (other.baseImage != null)
|
||||
return false;
|
||||
} else if (!baseImage.equals(other.baseImage))
|
||||
return false;
|
||||
if (decorators == null) {
|
||||
if (other.decorators != null)
|
||||
return false;
|
||||
} else if (!decorators.equals(other.decorators))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("baseImage", baseImage);
|
||||
b.add("decorators", decorators);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public IImageDescription getBaseImage() {
|
||||
return baseImage;
|
||||
}
|
||||
|
||||
public List<IImageDescription> getDecorators() {
|
||||
return decorators;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
|
||||
public class EClassImageDescriptionProvider implements IImageDescriptionProvider {
|
||||
@Override
|
||||
public IImageDescription getImageDescription(Object element) {
|
||||
if (element instanceof EClass) {
|
||||
List<String> images = new ArrayList<>();
|
||||
images.add(((EClass) element).getName());
|
||||
for (EClass superType : ((EClass) element).getEAllSuperTypes()) {
|
||||
images.add(superType.getName());
|
||||
}
|
||||
images.add(IImageDescription.DEFAULT.getImageID());
|
||||
return new AlternativeImageDescription(images);
|
||||
} else if (element instanceof EObject) {
|
||||
return getImageDescription(((EObject) element).eClass());
|
||||
} else if (element instanceof IEObjectDescription) {
|
||||
return getImageDescription(((IEObjectDescription) element).getEClass());
|
||||
} else {
|
||||
return IImageDescription.DEFAULT;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
|
@ -8,8 +8,6 @@
|
|||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import org.eclipse.xtext.ide.labels.SimpleImageDescription;
|
||||
|
||||
/**
|
||||
* Describes an image in a platform independent way.
|
||||
* Subclasses should implement {@link Object#equals(Object)} and {@link Object#hashCode()}.
|
||||
|
@ -18,7 +16,6 @@ import org.eclipse.xtext.ide.labels.SimpleImageDescription;
|
|||
* @noimplement
|
||||
* @noextend
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface IImageDescription {
|
||||
static final SimpleImageDescription DEFAULT = new SimpleImageDescription("default");
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.labels
|
||||
|
||||
import org.eclipse.xtend.lib.annotations.Data
|
||||
import java.util.List
|
||||
import com.google.common.collect.ImmutableList
|
||||
|
||||
/**
|
||||
* Describes an image in a platform independent way.
|
||||
* Subclasses should implement {@link Object#equals(Object)} and {@link Object#hashCode()}.
|
||||
*
|
||||
* @author Jan Koehnlein - Initial contribution and API
|
||||
* @noimplement
|
||||
* @noextend
|
||||
*/
|
||||
interface IImageDescription {
|
||||
val DEFAULT = new SimpleImageDescription('default')
|
||||
}
|
||||
|
||||
@Data
|
||||
class SimpleImageDescription implements IImageDescription {
|
||||
String imageID
|
||||
}
|
||||
|
||||
@Data
|
||||
class AlternativeImageDescription implements IImageDescription {
|
||||
List<String> imageIDs
|
||||
|
||||
new(Iterable<String> imageIDs) {
|
||||
this.imageIDs = ImmutableList.copyOf(imageIDs)
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
class DecoratedImageDescription implements IImageDescription {
|
||||
IImageDescription baseImage
|
||||
|
||||
List<IImageDescription> decorators
|
||||
|
||||
new(IImageDescription baseImage, IImageDescription... decorators) {
|
||||
this.baseImage = baseImage
|
||||
this.decorators = ImmutableList.copyOf(decorators)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
|
@ -9,14 +9,11 @@
|
|||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.inject.ImplementedBy;
|
||||
import org.eclipse.xtext.ide.labels.EClassImageDescriptionProvider;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
|
||||
/**
|
||||
* @author Jan Koehnlein - Initial contribution and API
|
||||
*/
|
||||
@ImplementedBy(EClassImageDescriptionProvider.class)
|
||||
@SuppressWarnings("all")
|
||||
public interface IImageDescriptionProvider {
|
||||
IImageDescription getImageDescription(final Object element);
|
||||
IImageDescription getImageDescription(Object element);
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.labels
|
||||
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import com.google.inject.ImplementedBy
|
||||
import org.eclipse.xtext.resource.IEObjectDescription
|
||||
import org.eclipse.emf.ecore.EClass
|
||||
|
||||
/**
|
||||
* @author Jan Koehnlein - Initial contribution and API
|
||||
*/
|
||||
@ImplementedBy(EClassImageDescriptionProvider)
|
||||
interface IImageDescriptionProvider {
|
||||
def IImageDescription getImageDescription(Object element)
|
||||
}
|
||||
|
||||
|
||||
class EClassImageDescriptionProvider implements IImageDescriptionProvider {
|
||||
|
||||
override getImageDescription(Object element) {
|
||||
switch element {
|
||||
EClass:
|
||||
new AlternativeImageDescription(#[element.name] + element.EAllSuperTypes.map[name] + #[IImageDescription.DEFAULT.imageID])
|
||||
EObject:
|
||||
element.eClass.imageDescription
|
||||
IEObjectDescription:
|
||||
element.EClass.imageDescription
|
||||
default:
|
||||
IImageDescription.DEFAULT
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
|
@ -9,13 +9,11 @@
|
|||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.inject.ImplementedBy;
|
||||
import org.eclipse.xtext.ide.labels.SimpleNameLabelProvider;
|
||||
|
||||
/**
|
||||
* @author Jan Koehnlein - Initial contribution and API
|
||||
*/
|
||||
@ImplementedBy(SimpleNameLabelProvider.class)
|
||||
@SuppressWarnings("all")
|
||||
public interface INameLabelProvider {
|
||||
String getNameLabel(final Object element);
|
||||
String getNameLabel(Object element);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.labels
|
||||
|
||||
import com.google.inject.ImplementedBy
|
||||
import org.eclipse.xtext.util.SimpleAttributeResolver
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.xtext.resource.IEObjectDescription
|
||||
|
||||
/**
|
||||
* @author Jan Koehnlein - Initial contribution and API
|
||||
*/
|
||||
@ImplementedBy(SimpleNameLabelProvider)
|
||||
interface INameLabelProvider {
|
||||
def String getNameLabel(Object element)
|
||||
}
|
||||
|
||||
class SimpleNameLabelProvider implements INameLabelProvider {
|
||||
|
||||
override getNameLabel(Object element) {
|
||||
switch element {
|
||||
EObject:
|
||||
SimpleAttributeResolver.NAME_RESOLVER.apply(element)
|
||||
IEObjectDescription:
|
||||
element.name.lastSegment
|
||||
default:
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
public class SimpleImageDescription implements IImageDescription {
|
||||
private final String imageID;
|
||||
|
||||
public SimpleImageDescription(String imageID) {
|
||||
this.imageID = imageID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((imageID == null) ? 0 : imageID.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SimpleImageDescription other = (SimpleImageDescription) obj;
|
||||
if (imageID == null) {
|
||||
if (other.imageID != null)
|
||||
return false;
|
||||
} else if (!imageID.equals(other.imageID))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("imageID", imageID);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public String getImageID() {
|
||||
return imageID;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.ide.labels.INameLabelProvider;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
import org.eclipse.xtext.util.SimpleAttributeResolver;
|
||||
|
||||
public class SimpleNameLabelProvider implements INameLabelProvider {
|
||||
|
||||
/**
|
||||
* Calculates the simple-name based on the type of the passed {@code element}. If the {@code element} is an instance
|
||||
* of {@code EObject}, the value of the {@code name} attribute is returned. If the {@code element} is an instance of
|
||||
* {@code IEObjectDescription}, the value of the {@link QualifiedName#getLastSegment()} is returned. A {@code null}
|
||||
* value is returned otherwise.
|
||||
*/
|
||||
@Override
|
||||
public String getNameLabel(Object element) {
|
||||
if (element instanceof EObject) {
|
||||
return SimpleAttributeResolver.NAME_RESOLVER.apply(((EObject) element));
|
||||
} else if (element instanceof IEObjectDescription) {
|
||||
return ((IEObjectDescription) element).getName().getLastSegment();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import com.google.inject.ImplementedBy;
|
||||
import com.google.inject.Inject;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.conversion.IValueConverterService;
|
||||
import org.eclipse.xtext.conversion.ValueConverterException;
|
||||
|
||||
import static org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity.*;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@ImplementedBy(IRenameNameValidator.RuleBasedNameValidator.class)
|
||||
public interface IRenameNameValidator {
|
||||
|
||||
void validate(EObject target, String newName, RefactoringIssueAcceptor issues);
|
||||
|
||||
class RuleBasedNameValidator implements IRenameNameValidator {
|
||||
|
||||
@Inject private IValueConverterService valueConverterService;
|
||||
|
||||
@Override
|
||||
public void validate(EObject target, String newName, RefactoringIssueAcceptor issues) {
|
||||
try {
|
||||
getNameAsValue(newName);
|
||||
} catch (ValueConverterException e) {
|
||||
issues.add(FATAL, "Illegal name: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected String getNameAsText(String nameAsValue) {
|
||||
return valueConverterService.toString(nameAsValue, getRuleName());
|
||||
}
|
||||
|
||||
protected String getNameAsValue(String nameAsText) {
|
||||
return valueConverterService.toValue(nameAsText, getRuleName(), null).toString();
|
||||
}
|
||||
|
||||
protected String getRuleName() {
|
||||
return "ID";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import com.google.inject.ImplementedBy
|
||||
import com.google.inject.Inject
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.xtext.conversion.IValueConverterService
|
||||
import org.eclipse.xtext.conversion.ValueConverterException
|
||||
|
||||
import static org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity.*
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@ImplementedBy(RuleBasedNameValidator)
|
||||
interface IRenameNameValidator {
|
||||
|
||||
def void validate(EObject target, String newName, RefactoringIssueAcceptor issues)
|
||||
|
||||
class RuleBasedNameValidator implements IRenameNameValidator {
|
||||
|
||||
@Inject IValueConverterService valueConverterService
|
||||
|
||||
override validate(EObject target, String newName, RefactoringIssueAcceptor issues) {
|
||||
try {
|
||||
getNameAsValue(newName)
|
||||
} catch (ValueConverterException e) {
|
||||
issues.add(FATAL, '''Illegal name: «e.message»''')
|
||||
}
|
||||
}
|
||||
|
||||
protected def String getNameAsText(String nameAsValue) {
|
||||
valueConverterService.toString(nameAsValue, ruleName)
|
||||
}
|
||||
|
||||
protected def String getNameAsValue(String nameAsText) {
|
||||
return valueConverterService.toValue(nameAsText, ruleName, null).toString()
|
||||
}
|
||||
|
||||
protected def getRuleName() {
|
||||
'ID'
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.emf.ecore.EAttribute;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* Called to rename an element in the {@link IChangeSerializer} based refactoring.
|
||||
*
|
||||
* Clients may extend the {@link DefaultImpl} to customize the behavior or implement this interface directly.
|
||||
*
|
||||
* Changes are usually performed in the The {@link RenameContext}
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public interface IRenameStrategy2 {
|
||||
class DefaultImpl implements IRenameStrategy2 {
|
||||
@Inject
|
||||
private IResourceServiceProvider resourceServiceProvider;
|
||||
|
||||
public boolean canHandle(RenameChange change) {
|
||||
return resourceServiceProvider.canHandle(change.getTargetURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyRename(RenameContext context) {
|
||||
for (RenameChange change : context.getChanges()) {
|
||||
if (canHandle(change)) {
|
||||
context.addModification(change, (EObject o) -> doRename(o, change, context));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void doRename(EObject target, RenameChange change, RenameContext context) {
|
||||
EAttribute nameAttribute = getNameEAttribute(target);
|
||||
if (nameAttribute != null) {
|
||||
target.eSet(nameAttribute, change.getNewName());
|
||||
} else {
|
||||
context.getIssues().add(RefactoringIssueAcceptor.Severity.WARNING, "Element of class " + target.eClass().getName() + " cannot be renamed.");
|
||||
}
|
||||
}
|
||||
|
||||
protected EAttribute getNameEAttribute(EObject target) {
|
||||
for (EAttribute attribute : target.eClass().getEAllAttributes()) {
|
||||
if ("name".equals(attribute.getName()) && EcorePackage.Literals.ESTRING == attribute.getEType()) {
|
||||
return attribute;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void applyRename(RenameContext context);
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import com.google.inject.Inject
|
||||
import org.eclipse.emf.ecore.EAttribute
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.EcorePackage
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider
|
||||
|
||||
/**
|
||||
* Called to rename an element in the {@link IChangeSerializer} based refactoring.
|
||||
*
|
||||
* Clients may extend the {@link DefaultImpl} to customize the behavior or implement
|
||||
* this interface directly.
|
||||
*
|
||||
* Changes are usually performed in the The {@link RenameContext}
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
interface IRenameStrategy2 {
|
||||
|
||||
def void applyRename(RenameContext context)
|
||||
|
||||
class DefaultImpl implements IRenameStrategy2 {
|
||||
|
||||
@Inject IResourceServiceProvider resourceServiceProvider
|
||||
|
||||
def boolean canHandle(RenameChange change) {
|
||||
resourceServiceProvider.canHandle(change.targetURI)
|
||||
}
|
||||
|
||||
override applyRename(RenameContext context) {
|
||||
context.changes.filter[ canHandle ].forEach [ change |
|
||||
context.addModification(change) [ doRename(change, context) ]
|
||||
]
|
||||
}
|
||||
|
||||
protected def void doRename(EObject target, RenameChange change, RenameContext context) {
|
||||
val nameAttribute = getNameEAttribute(target)
|
||||
if (nameAttribute !== null)
|
||||
target.eSet(nameAttribute, change.newName)
|
||||
else
|
||||
context.issues.add(Severity.WARNING, 'Element of class ' + target.eClass.name + ' cannot be renamed.')
|
||||
}
|
||||
|
||||
protected def EAttribute getNameEAttribute(EObject target) {
|
||||
target.eClass.EAllAttributes.filter[name == 'name' && EType == EcorePackage.Literals.ESTRING].head
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
/**
|
||||
* Allows a language to execute side-effects when the URI of a resource changes.
|
||||
|
@ -26,9 +26,9 @@ package org.eclipse.xtext.ide.refactoring
|
|||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
interface IResourceRelocationStrategy {
|
||||
public interface IResourceRelocationStrategy {
|
||||
|
||||
def void applyChange(ResourceRelocationContext context)
|
||||
void applyChange(ResourceRelocationContext context);
|
||||
|
||||
/**
|
||||
* By overriding this method client implementations may explicitly demand for relying
|
||||
|
@ -50,7 +50,7 @@ interface IResourceRelocationStrategy {
|
|||
*
|
||||
* @since 2.18
|
||||
*/
|
||||
def boolean requiresUsageOfPersistedIndex(ResourceRelocationContext context) {
|
||||
default boolean requiresUsageOfPersistedIndex(ResourceRelocationContext context) {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public interface RefactoringIssueAcceptor {
|
||||
enum Severity {
|
||||
FATAL, ERROR, WARNING, INFO, OK;
|
||||
}
|
||||
|
||||
void add(Severity severity, String message, URI uri, ResourceSet resourceSet);
|
||||
|
||||
void add(Severity severity, String message, URI resourceUri);
|
||||
|
||||
void add(Severity severity, String message, EObject element);
|
||||
|
||||
void add(Severity severity, String message, EObject element, ITextRegion region);
|
||||
|
||||
void add(Severity severity, String message, Exception exc, Logger log);
|
||||
|
||||
void add(Severity severity, String message, Object... params);
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017, 2018 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import org.apache.log4j.Logger
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.xtext.util.ITextRegion
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
interface RefactoringIssueAcceptor {
|
||||
|
||||
enum Severity {
|
||||
FATAL, ERROR, WARNING, INFO, OK
|
||||
}
|
||||
|
||||
def void add(Severity severity, String message, URI uri, ResourceSet resourceSet)
|
||||
|
||||
def void add(Severity severity, String message, URI resourceUri)
|
||||
|
||||
def void add(Severity severity, String message, EObject element)
|
||||
|
||||
def void add(Severity severity, String message, EObject element, ITextRegion region)
|
||||
|
||||
def void add(Severity severity, String message, Exception exc, Logger log)
|
||||
|
||||
def void add(Severity severity, String message, Object... params)
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public class RenameChange {
|
||||
private final String newName;
|
||||
|
||||
private final URI targetURI;
|
||||
|
||||
public RenameChange(String newName, URI targetURI) {
|
||||
this.newName = newName;
|
||||
this.targetURI = targetURI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((newName == null) ? 0 : newName.hashCode());
|
||||
result = prime * result + ((targetURI == null) ? 0 : targetURI.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
RenameChange other = (RenameChange) obj;
|
||||
if (newName == null) {
|
||||
if (other.newName != null)
|
||||
return false;
|
||||
} else if (!newName.equals(other.newName))
|
||||
return false;
|
||||
if (targetURI == null) {
|
||||
if (other.targetURI != null)
|
||||
return false;
|
||||
} else if (!targetURI.equals(other.targetURI))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("newName", newName);
|
||||
b.add("targetURI", targetURI);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public String getNewName() {
|
||||
return newName;
|
||||
}
|
||||
|
||||
public URI getTargetURI() {
|
||||
return targetURI;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.xtend.lib.annotations.Data
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@Data
|
||||
class RenameChange {
|
||||
String newName
|
||||
URI targetURI
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public class RenameContext {
|
||||
private final List<? extends RenameChange> changes;
|
||||
|
||||
private final ResourceSet resourceSet;
|
||||
|
||||
private final IChangeSerializer changeSerializer;
|
||||
|
||||
private final RefactoringIssueAcceptor issues;
|
||||
|
||||
public RenameContext(List<? extends RenameChange> changes, ResourceSet resourceSet,
|
||||
IChangeSerializer changeSerializer, RefactoringIssueAcceptor issues) {
|
||||
this.changes = changes;
|
||||
this.resourceSet = resourceSet;
|
||||
this.changeSerializer = changeSerializer;
|
||||
this.issues = issues;
|
||||
}
|
||||
|
||||
public void addModification(RenameChange change, IChangeSerializer.IModification<EObject> modification) {
|
||||
EObject target = resourceSet.getEObject(change.getTargetURI(), true);
|
||||
if (target != null) {
|
||||
EcoreUtil.resolveAll(target.eResource());
|
||||
changeSerializer.addModification(target, modification);
|
||||
} else {
|
||||
issues.add(RefactoringIssueAcceptor.Severity.ERROR, "Element cannot be found", change.getTargetURI());
|
||||
}
|
||||
}
|
||||
|
||||
public List<? extends RenameChange> getChanges() {
|
||||
return changes;
|
||||
}
|
||||
|
||||
public ResourceSet getResourceSet() {
|
||||
return resourceSet;
|
||||
}
|
||||
|
||||
public IChangeSerializer getChangeSerializer() {
|
||||
return changeSerializer;
|
||||
}
|
||||
|
||||
public RefactoringIssueAcceptor getIssues() {
|
||||
return issues;
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import java.util.List
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@FinalFieldsConstructor
|
||||
@Accessors(PUBLIC_GETTER)
|
||||
class RenameContext {
|
||||
val List<? extends RenameChange> changes
|
||||
val ResourceSet resourceSet
|
||||
val IChangeSerializer changeSerializer
|
||||
val RefactoringIssueAcceptor issues
|
||||
|
||||
def void addModification(RenameChange change, IChangeSerializer.IModification<EObject> modification) {
|
||||
val target = resourceSet.getEObject(change.targetURI, true)
|
||||
if (target !== null) {
|
||||
EcoreUtil.resolveAll(target.eResource)
|
||||
changeSerializer.addModification(target, modification)
|
||||
} else {
|
||||
issues.add(Severity.ERROR, 'Element cannot be found', change.targetURI)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
/**
|
||||
* URIs can also refer to folders and non-Xtext resources.
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public class ResourceRelocationChange {
|
||||
private final URI fromURI;
|
||||
|
||||
private final URI toURI;
|
||||
|
||||
private final boolean isFile;
|
||||
|
||||
public ResourceRelocationChange(URI fromURI, URI toURI, boolean isFile) {
|
||||
this.fromURI = fromURI;
|
||||
this.toURI = toURI;
|
||||
this.isFile = isFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((fromURI == null) ? 0 : fromURI.hashCode());
|
||||
result = prime * result + (isFile ? 1231 : 1237);
|
||||
result = prime * result + ((toURI == null) ? 0 : toURI.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ResourceRelocationChange other = (ResourceRelocationChange) obj;
|
||||
if (fromURI == null) {
|
||||
if (other.fromURI != null)
|
||||
return false;
|
||||
} else if (!fromURI.equals(other.fromURI))
|
||||
return false;
|
||||
if (isFile != other.isFile)
|
||||
return false;
|
||||
if (toURI == null) {
|
||||
if (other.toURI != null)
|
||||
return false;
|
||||
} else if (!toURI.equals(other.toURI))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("fromURI", fromURI);
|
||||
b.add("toURI", toURI);
|
||||
b.add("isFile", isFile);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public URI getFromURI() {
|
||||
return fromURI;
|
||||
}
|
||||
|
||||
public URI getToURI() {
|
||||
return toURI;
|
||||
}
|
||||
|
||||
public boolean isFile() {
|
||||
return isFile;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.xtend.lib.annotations.Data
|
||||
|
||||
/**
|
||||
* URIs can also refer to folders and non-Xtext resources.
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@Data
|
||||
class ResourceRelocationChange {
|
||||
URI fromURI
|
||||
URI toURI
|
||||
boolean isFile
|
||||
}
|
|
@ -1,21 +1,21 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import org.eclipse.xtext.resource.IResourceDescription
|
||||
import java.util.List
|
||||
import org.eclipse.xtext.resource.IResourceDescription;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*/
|
||||
interface BuildListener {
|
||||
|
||||
def void afterBuild(List<IResourceDescription.Delta> deltas)
|
||||
|
||||
}
|
||||
public interface BuildListener {
|
||||
|
||||
void afterBuild(List<IResourceDescription.Delta> deltas);
|
||||
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.lsp4j.Location;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.util.LineAndColumn;
|
||||
|
||||
import static org.eclipse.xtext.nodemodel.util.NodeModelUtils.*;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
public class DocumentExtensions {
|
||||
|
||||
@Inject
|
||||
private UriExtensions uriExtensions;
|
||||
|
||||
@Inject
|
||||
private ILocationInFileProvider locationInFileProvider;
|
||||
|
||||
public Position newPosition(Resource resource, int offset) {
|
||||
if (resource instanceof XtextResource) {
|
||||
ICompositeNode rootNode = ((XtextResource) resource).getParseResult().getRootNode();
|
||||
LineAndColumn lineAndColumn = getLineAndColumn(rootNode, offset);
|
||||
return new Position(lineAndColumn.getLine() - 1, lineAndColumn.getColumn() - 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Range newRange(Resource resource, int startOffset, int endOffset) {
|
||||
Position startPosition = newPosition(resource, startOffset);
|
||||
if (startPosition == null) {
|
||||
return null;
|
||||
}
|
||||
Position endPosition = newPosition(resource, endOffset);
|
||||
if (endPosition == null) {
|
||||
return null;
|
||||
}
|
||||
return new Range(startPosition, endPosition);
|
||||
}
|
||||
|
||||
public Range newRange(Resource resource, ITextRegion region) {
|
||||
if (region == null)
|
||||
return null;
|
||||
return newRange(resource, region.getOffset(), region.getOffset() + region.getLength());
|
||||
}
|
||||
|
||||
public Location newLocation(Resource resource, ITextRegion textRegion) {
|
||||
Range range = newRange(resource, textRegion);
|
||||
if (range == null) {
|
||||
return null;
|
||||
}
|
||||
String uri = uriExtensions.toUriString(resource.getURI());
|
||||
return new Location(uri, range);
|
||||
}
|
||||
|
||||
public Location newLocation(EObject object) {
|
||||
Resource resource = object.eResource();
|
||||
ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(object);
|
||||
return newLocation(resource, textRegion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns with the {@link Location location} that represents the {@link ILocationInFileProvider#getFullTextRegion
|
||||
* full text region} of the argument.
|
||||
*
|
||||
* @since 2.16
|
||||
*/
|
||||
public Location newFullLocation(EObject object) {
|
||||
Resource resource = object.eResource();
|
||||
ITextRegion textRegion = locationInFileProvider.getFullTextRegion(object);
|
||||
return newLocation(resource, textRegion);
|
||||
}
|
||||
|
||||
public Location newLocation(EObject owner, EStructuralFeature feature, int indexInList) {
|
||||
Resource resource = owner.eResource();
|
||||
ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(owner, feature, indexInList);
|
||||
return newLocation(resource, textRegion);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server
|
||||
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Singleton
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.EStructuralFeature
|
||||
import org.eclipse.emf.ecore.resource.Resource
|
||||
import org.eclipse.lsp4j.Location
|
||||
import org.eclipse.lsp4j.Position
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider
|
||||
import org.eclipse.xtext.resource.XtextResource
|
||||
import org.eclipse.xtext.util.ITextRegion
|
||||
|
||||
import static extension org.eclipse.xtext.nodemodel.util.NodeModelUtils.*
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
class DocumentExtensions {
|
||||
|
||||
@Inject
|
||||
extension UriExtensions
|
||||
|
||||
@Inject
|
||||
ILocationInFileProvider locationInFileProvider
|
||||
|
||||
def Position newPosition(Resource resource, int offset) {
|
||||
if (resource instanceof XtextResource) {
|
||||
val rootNode = resource.parseResult.rootNode
|
||||
val lineAndColumn = rootNode.getLineAndColumn(offset)
|
||||
return new Position(lineAndColumn.line - 1, lineAndColumn.column - 1)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
def Range newRange(Resource resource, int startOffset, int endOffset) {
|
||||
val startPosition = resource.newPosition(startOffset)
|
||||
if (startPosition === null) {
|
||||
return null
|
||||
}
|
||||
val endPosition = resource.newPosition(endOffset)
|
||||
if (endPosition === null) {
|
||||
return null
|
||||
}
|
||||
return new Range(startPosition, endPosition)
|
||||
}
|
||||
|
||||
def Range newRange(Resource resource, ITextRegion region) {
|
||||
if (region === null) return null
|
||||
return resource.newRange(region.offset, region.offset + region.length)
|
||||
}
|
||||
|
||||
def Location newLocation(Resource resource, ITextRegion textRegion) {
|
||||
val range = resource.newRange(textRegion)
|
||||
if (range === null) {
|
||||
return null
|
||||
}
|
||||
val uri = resource.URI.toUriString
|
||||
return new Location(uri, range)
|
||||
}
|
||||
|
||||
def Location newLocation(EObject object) {
|
||||
val resource = object.eResource
|
||||
val textRegion = locationInFileProvider.getSignificantTextRegion(object)
|
||||
return resource.newLocation(textRegion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns with the {@link Location location} that represents the {@link ILocationInFileProvider#getFullTextRegion full text region}
|
||||
* of the argument.
|
||||
*
|
||||
* @since 2.16
|
||||
*/
|
||||
def Location newFullLocation(EObject object) {
|
||||
val resource = object.eResource
|
||||
val textRegion = locationInFileProvider.getFullTextRegion(object)
|
||||
return resource.newLocation(textRegion)
|
||||
}
|
||||
|
||||
def Location newLocation(EObject owner, EStructuralFeature feature, int indexInList) {
|
||||
val resource = owner.eResource
|
||||
val textRegion = locationInFileProvider.getSignificantTextRegion(owner, feature, indexInList)
|
||||
return resource.newLocation(textRegion)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +1,15 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import com.google.inject.Singleton
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
/**
|
||||
* Normalizes file uris without authorities (<code>file:/path...</code>) to contain an empty authority (i.e. starts with three slashes:<code>file:///path...</code>).
|
||||
|
@ -18,20 +18,20 @@ import org.eclipse.emf.common.util.URI
|
|||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
class UriExtensions extends org.eclipse.xtext.util.UriExtensions {
|
||||
public class UriExtensions extends org.eclipse.xtext.util.UriExtensions {
|
||||
|
||||
/**
|
||||
* returns the string representation of the given URI (with empty authority, if absent and has file scheme).
|
||||
*/
|
||||
def String toUriString(URI uri) {
|
||||
return uri.withEmptyAuthority.toString
|
||||
public String toUriString(URI uri) {
|
||||
return withEmptyAuthority(uri).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a java.net.URI into a string representation with empty authority, if absent and has file scheme.
|
||||
*/
|
||||
def String toUriString(java.net.URI uri) {
|
||||
return toUriString(URI.createURI(uri.normalize.toString))
|
||||
public String toUriString(java.net.URI uri) {
|
||||
return toUriString(URI.createURI(uri.normalize().toString()));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 itemis AG (http://www.itemis.com) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.formatting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.lsp4j.DocumentFormattingParams;
|
||||
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
|
||||
import org.eclipse.lsp4j.FormattingOptions;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.TextEdit;
|
||||
import org.eclipse.xtext.formatting.IIndentationInformation;
|
||||
import org.eclipse.xtext.formatting2.FormatterRequest;
|
||||
import org.eclipse.xtext.formatting2.IFormatter2;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.ITextReplacement;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.preferences.ITypedPreferenceValues;
|
||||
import org.eclipse.xtext.preferences.MapBasedPreferenceValues;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.util.TextRegion;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
/**
|
||||
* Language Service Implementation for Formatting and Range-Formatting
|
||||
*
|
||||
* @author Christian Dietrich - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
public class FormattingService {
|
||||
@Inject(optional = true)
|
||||
private Provider<IFormatter2> formatter2Provider;
|
||||
|
||||
@Inject
|
||||
private Provider<FormatterRequest> formatterRequestProvider;
|
||||
|
||||
@Inject
|
||||
private TextRegionAccessBuilder regionBuilder;
|
||||
|
||||
@Inject
|
||||
private IIndentationInformation indentationInformation;
|
||||
|
||||
public List<? extends TextEdit> format(Document document, XtextResource resource, DocumentFormattingParams params,
|
||||
CancelIndicator cancelIndicator) {
|
||||
int offset = 0;
|
||||
int length = document.getContents().length();
|
||||
if (length == 0 || resource.getContents().isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return format(resource, document, offset, length, params.getOptions());
|
||||
}
|
||||
|
||||
public List<? extends TextEdit> format(Document document, XtextResource resource,
|
||||
DocumentRangeFormattingParams params, CancelIndicator cancelIndicator) {
|
||||
int startOffset = document.getOffSet(params.getRange().getStart());
|
||||
int endOffset = document.getOffSet(params.getRange().getEnd());
|
||||
int length = endOffset - startOffset;
|
||||
return format(resource, document, startOffset, length, params.getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.14
|
||||
*/
|
||||
public List<TextEdit> format(XtextResource resource, Document document, int offset, int length,
|
||||
FormattingOptions options) {
|
||||
String indent = indentationInformation.getIndentString();
|
||||
if (options != null) {
|
||||
if (options.isInsertSpaces()) {
|
||||
indent = Strings.padEnd("", options.getTabSize(), ' ');
|
||||
}
|
||||
}
|
||||
List<TextEdit> result = new ArrayList<>();
|
||||
if (this.formatter2Provider != null) {
|
||||
MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
|
||||
preferences.put("indentation", indent);
|
||||
List<ITextReplacement> replacements = format2(resource, new TextRegion(offset, length), preferences);
|
||||
for (ITextReplacement r : replacements) {
|
||||
result.add(toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected TextEdit toTextEdit(Document document, String formattedText, int startOffset, int length) {
|
||||
TextEdit textEdit = new TextEdit();
|
||||
textEdit.setNewText(formattedText);
|
||||
textEdit.setRange(new Range(document.getPosition(startOffset), document.getPosition((startOffset + length))));
|
||||
return textEdit;
|
||||
}
|
||||
|
||||
protected List<ITextReplacement> format2(XtextResource resource, ITextRegion selection,
|
||||
ITypedPreferenceValues preferences) {
|
||||
FormatterRequest request = formatterRequestProvider.get();
|
||||
request.setAllowIdentityEdits(false);
|
||||
request.setFormatUndefinedHiddenRegionsOnly(false);
|
||||
if (selection != null) {
|
||||
request.setRegions(Collections.singletonList(selection));
|
||||
}
|
||||
if (preferences != null) {
|
||||
request.setPreferences(preferences);
|
||||
}
|
||||
ITextRegionAccess regionAccess = regionBuilder.forNodeModel(resource).create();
|
||||
request.setTextRegionAccess(regionAccess);
|
||||
IFormatter2 formatter2 = formatter2Provider.get();
|
||||
List<ITextReplacement> replacements = formatter2.format(request);
|
||||
return replacements;
|
||||
}
|
||||
}
|
|
@ -1,122 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2018 itemis AG (http://www.itemis.com) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server.formatting
|
||||
|
||||
import com.google.common.base.Strings
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Provider
|
||||
import java.util.List
|
||||
import org.eclipse.lsp4j.DocumentFormattingParams
|
||||
import org.eclipse.lsp4j.DocumentRangeFormattingParams
|
||||
import org.eclipse.lsp4j.FormattingOptions
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.eclipse.lsp4j.TextEdit
|
||||
import org.eclipse.xtext.formatting.IIndentationInformation
|
||||
import org.eclipse.xtext.formatting2.FormatterRequest
|
||||
import org.eclipse.xtext.formatting2.IFormatter2
|
||||
import org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder
|
||||
import org.eclipse.xtext.ide.server.Document
|
||||
import org.eclipse.xtext.preferences.ITypedPreferenceValues
|
||||
import org.eclipse.xtext.preferences.MapBasedPreferenceValues
|
||||
import org.eclipse.xtext.resource.XtextResource
|
||||
import org.eclipse.xtext.util.CancelIndicator
|
||||
import org.eclipse.xtext.util.ITextRegion
|
||||
import org.eclipse.xtext.util.TextRegion
|
||||
|
||||
/**
|
||||
* Language Service Implementation for Formatting and Range-Formatting
|
||||
*
|
||||
* @author Christian Dietrich - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
class FormattingService {
|
||||
|
||||
@Inject(optional=true) Provider<IFormatter2> formatter2Provider
|
||||
|
||||
@Inject Provider<FormatterRequest> formatterRequestProvider
|
||||
|
||||
@Inject TextRegionAccessBuilder regionBuilder
|
||||
|
||||
@Inject IIndentationInformation indentationInformation
|
||||
|
||||
def List<? extends TextEdit> format(
|
||||
Document document,
|
||||
XtextResource resource,
|
||||
DocumentFormattingParams params,
|
||||
CancelIndicator cancelIndicator
|
||||
) {
|
||||
val offset = 0
|
||||
val length = document.contents.length
|
||||
if (length === 0 || resource.contents.isEmpty) {
|
||||
return emptyList
|
||||
}
|
||||
return format(resource, document, offset, length, params.options)
|
||||
}
|
||||
|
||||
def List<? extends TextEdit> format(
|
||||
Document document,
|
||||
XtextResource resource,
|
||||
DocumentRangeFormattingParams params,
|
||||
CancelIndicator cancelIndicator
|
||||
) {
|
||||
val offset = document.getOffSet(params.range.start)
|
||||
val length = document.getOffSet(params.range.end) - offset
|
||||
return format(resource, document, offset, length, params.options)
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.14
|
||||
*/
|
||||
def List<TextEdit> format(XtextResource resource, Document document, int offset, int length, FormattingOptions options) {
|
||||
var indent = indentationInformation.indentString
|
||||
if (options !== null) {
|
||||
if (options.insertSpaces) {
|
||||
indent = Strings.padEnd("", options.tabSize," ")
|
||||
}
|
||||
}
|
||||
if (formatter2Provider !== null) {
|
||||
val preferences = new MapBasedPreferenceValues()
|
||||
preferences.put("indentation", indent)
|
||||
val replacements = format2(resource, new TextRegion(offset, length), preferences)
|
||||
return replacements.map [ r |
|
||||
document.toTextEdit(r.replacementText, r.offset, r.length)
|
||||
].<TextEdit>toList
|
||||
} else {
|
||||
return <TextEdit>newArrayList
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected def TextEdit toTextEdit(Document document, String formattedText, int startOffset, int length) {
|
||||
new TextEdit => [
|
||||
newText = formattedText
|
||||
range = new Range => [
|
||||
start = document.getPosition(startOffset)
|
||||
end = document.getPosition(startOffset + length)
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
protected def format2(XtextResource resource, ITextRegion selection, ITypedPreferenceValues preferences) {
|
||||
val request = formatterRequestProvider.get()
|
||||
request.allowIdentityEdits = false
|
||||
request.formatUndefinedHiddenRegionsOnly = false
|
||||
if (selection !== null)
|
||||
request.regions = #[selection]
|
||||
if (preferences !== null) {
|
||||
request.preferences = preferences
|
||||
}
|
||||
val regionAccess = regionBuilder.forNodeModel(resource).create()
|
||||
request.textRegionAccess = regionAccess
|
||||
val formatter2 = formatter2Provider.get();
|
||||
val replacements = formatter2.format(request)
|
||||
return replacements
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.hover;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.HoverParams;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.ide.server.DocumentExtensions;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.eclipse.xtext.parser.IParseResult;
|
||||
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@Beta
|
||||
public class HoverService implements IHoverService {
|
||||
@Inject
|
||||
private DocumentExtensions documentExtensions;
|
||||
|
||||
@Inject
|
||||
private EObjectAtOffsetHelper eObjectAtOffsetHelper;
|
||||
|
||||
@Inject
|
||||
private ILocationInFileProvider locationInFileProvider;
|
||||
|
||||
@Inject
|
||||
private IEObjectDocumentationProvider eObjectDocumentationProvider;
|
||||
|
||||
@Override
|
||||
public Hover hover(Document document, XtextResource resource, HoverParams params, CancelIndicator cancelIndicator) {
|
||||
int offset = document.getOffSet(params.getPosition());
|
||||
HoverContext context = createContext(document, resource, offset);
|
||||
return hover(context);
|
||||
}
|
||||
|
||||
protected HoverContext createContext(Document document, XtextResource resource, int offset) {
|
||||
EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
|
||||
if (crossLinkedEObject != null) {
|
||||
if (crossLinkedEObject.eIsProxy()) {
|
||||
return null;
|
||||
}
|
||||
IParseResult parseResult = resource.getParseResult();
|
||||
if (parseResult == null) {
|
||||
return null;
|
||||
}
|
||||
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
|
||||
if (leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
|
||||
leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
|
||||
}
|
||||
if (leafNode == null) {
|
||||
return null;
|
||||
}
|
||||
ITextRegion leafRegion = leafNode.getTextRegion();
|
||||
return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
|
||||
}
|
||||
EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
ITextRegion region = locationInFileProvider.getSignificantTextRegion(element);
|
||||
return new HoverContext(document, resource, offset, region, element);
|
||||
}
|
||||
|
||||
protected Hover hover(HoverContext context) {
|
||||
if (context == null) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
MarkupContent contents = getMarkupContent(context);
|
||||
if (contents == null) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
Range range = getRange(context);
|
||||
if (range == null) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
return new Hover(contents, range);
|
||||
}
|
||||
|
||||
protected Range getRange(HoverContext ctx) {
|
||||
if (!ctx.getRegion().contains(ctx.getOffset())) {
|
||||
return null;
|
||||
}
|
||||
return documentExtensions.newRange(ctx.getResource(), ctx.getRegion());
|
||||
}
|
||||
|
||||
protected MarkupContent getMarkupContent(HoverContext ctx) {
|
||||
return toMarkupContent(getKind(ctx), getContents(ctx.getElement()));
|
||||
}
|
||||
|
||||
protected String getKind(HoverContext it) {
|
||||
return "markdown";
|
||||
}
|
||||
|
||||
protected MarkupContent toMarkupContent(String kind, String value) {
|
||||
return new MarkupContent(kind, value);
|
||||
}
|
||||
|
||||
public String getContents(EObject element) {
|
||||
if (element == null) {
|
||||
return "";
|
||||
}
|
||||
String documentation = eObjectDocumentationProvider.getDocumentation(element);
|
||||
if (documentation == null) {
|
||||
return "";
|
||||
}
|
||||
return documentation;
|
||||
}
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server.hover
|
||||
|
||||
import com.google.common.annotations.Beta
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Singleton
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.lsp4j.Hover
|
||||
import org.eclipse.lsp4j.HoverParams
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider
|
||||
import org.eclipse.xtext.ide.server.Document
|
||||
import org.eclipse.xtext.ide.server.DocumentExtensions
|
||||
import org.eclipse.xtext.resource.EObjectAtOffsetHelper
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider
|
||||
import org.eclipse.xtext.resource.XtextResource
|
||||
import org.eclipse.xtext.util.CancelIndicator
|
||||
|
||||
import static extension org.eclipse.xtext.nodemodel.util.NodeModelUtils.*
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@Beta
|
||||
class HoverService implements IHoverService {
|
||||
|
||||
@Inject
|
||||
extension DocumentExtensions
|
||||
|
||||
@Inject
|
||||
extension EObjectAtOffsetHelper
|
||||
|
||||
@Inject
|
||||
extension ILocationInFileProvider
|
||||
|
||||
@Inject
|
||||
extension IEObjectDocumentationProvider
|
||||
|
||||
override Hover hover(
|
||||
Document document,
|
||||
XtextResource resource,
|
||||
HoverParams params,
|
||||
CancelIndicator cancelIndicator
|
||||
) {
|
||||
val offset = document.getOffSet(params.position)
|
||||
val context = createContext(document, resource, offset)
|
||||
return context.hover
|
||||
}
|
||||
|
||||
protected def HoverContext createContext(Document document, XtextResource resource, int offset) {
|
||||
val crossLinkedEObject = resource.resolveCrossReferencedElementAt(offset)
|
||||
if (crossLinkedEObject !== null) {
|
||||
if(crossLinkedEObject.eIsProxy) return null
|
||||
|
||||
val parseResult = resource.parseResult
|
||||
if(parseResult === null) return null
|
||||
|
||||
var leafNode = parseResult.rootNode.findLeafNodeAtOffset(offset)
|
||||
if (leafNode !== null && leafNode.hidden && leafNode.offset == offset) {
|
||||
leafNode = parseResult.rootNode.findLeafNodeAtOffset(offset - 1)
|
||||
}
|
||||
if(leafNode === null) return null
|
||||
|
||||
val leafRegion = leafNode.textRegion
|
||||
return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject)
|
||||
}
|
||||
val element = resource.resolveElementAt(offset);
|
||||
if(element === null) return null
|
||||
|
||||
val region = element.significantTextRegion
|
||||
return new HoverContext(document, resource, offset, region, element)
|
||||
}
|
||||
|
||||
protected def Hover hover(HoverContext context) {
|
||||
if (context === null) return EMPTY_HOVER
|
||||
|
||||
val contents = context.markupContent
|
||||
if(contents === null) return EMPTY_HOVER
|
||||
|
||||
val range = context.range
|
||||
if(range === null) return EMPTY_HOVER
|
||||
|
||||
return new Hover(contents, range)
|
||||
}
|
||||
|
||||
protected def Range getRange(HoverContext it) {
|
||||
if(!region.contains(offset)) return null
|
||||
|
||||
return resource.newRange(region)
|
||||
}
|
||||
|
||||
protected def MarkupContent getMarkupContent(HoverContext it) {
|
||||
return toMarkupContent(kind, element.contents)
|
||||
}
|
||||
|
||||
protected def String getKind(HoverContext it) {
|
||||
return "markdown"
|
||||
}
|
||||
|
||||
protected def MarkupContent toMarkupContent(String kind, String value) {
|
||||
return new MarkupContent(kind, value)
|
||||
}
|
||||
|
||||
def String getContents(EObject element) {
|
||||
if(element === null) return ""
|
||||
|
||||
val documentation = element.documentation
|
||||
if(documentation === null) return ""
|
||||
|
||||
return documentation
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.rename;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
public class ServerRefactoringIssueAcceptor implements RefactoringIssueAcceptor {
|
||||
public static class Issue {
|
||||
private Severity severity;
|
||||
|
||||
private String message;
|
||||
}
|
||||
|
||||
private List<Issue> issues = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, URI uri, ResourceSet resourceSet) {
|
||||
addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, URI resourceUri) {
|
||||
addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, EObject element) {
|
||||
addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, EObject element, ITextRegion region) {
|
||||
addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, Exception exc, Logger log) {
|
||||
addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Severity severity, String message, Object... params) {
|
||||
Issue issue = new Issue();
|
||||
issue.severity = severity;
|
||||
issue.message = message;
|
||||
issues.add(issue);
|
||||
}
|
||||
|
||||
protected boolean addIssue(Severity severity, String message) {
|
||||
Issue issue = new Issue();
|
||||
issue.severity = severity;
|
||||
issue.message = message;
|
||||
return issues.add(issue);
|
||||
}
|
||||
|
||||
public Severity getMaximumSeverity() {
|
||||
if (issues.size() > 0) {
|
||||
Issue minBySeverity = IterableExtensions.minBy(issues, (i) -> i.severity);
|
||||
Severity severity = null;
|
||||
if (minBySeverity != null) {
|
||||
severity = minBySeverity.severity;
|
||||
}
|
||||
return severity;
|
||||
} else {
|
||||
return Severity.OK;
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseError toResponseError() {
|
||||
Severity maxSeverity = getMaximumSeverity();
|
||||
ResponseError responseError = new ResponseError();
|
||||
responseError.setMessage(getMessageBySeverity(maxSeverity));
|
||||
responseError.setCode(getCodeBySeverity(maxSeverity));
|
||||
List<Issue> bySeverity = IterableExtensions.sortBy(issues, (i) -> i.severity);
|
||||
List<String> messages = ListExtensions.map(ListExtensions.reverse(bySeverity), (i) -> i.message);
|
||||
responseError.setData(IterableExtensions.join(messages, "\n"));
|
||||
return responseError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.22
|
||||
*/
|
||||
protected int getCodeBySeverity(Severity maxSeverity) {
|
||||
if (maxSeverity != null) {
|
||||
switch (maxSeverity) {
|
||||
case OK:
|
||||
return 0;
|
||||
case INFO:
|
||||
return 0;
|
||||
case WARNING:
|
||||
return 0;
|
||||
case ERROR:
|
||||
return ResponseErrorCode.UnknownErrorCode.getValue();
|
||||
case FATAL:
|
||||
return ResponseErrorCode.UnknownErrorCode.getValue();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.22
|
||||
*/
|
||||
protected String getMessageBySeverity(Severity maxSeverity) {
|
||||
if (maxSeverity != null) {
|
||||
switch (maxSeverity) {
|
||||
case OK:
|
||||
return "Refactoring is possible";
|
||||
case INFO:
|
||||
return "Refactoring is possible";
|
||||
case WARNING:
|
||||
return "Refactoring could cause issues";
|
||||
case ERROR:
|
||||
return "Refactoring has errors";
|
||||
case FATAL:
|
||||
return "Refactoring cannot be performed";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkSeverity() {
|
||||
if (getMaximumSeverity().compareTo(Severity.WARNING) < 0) {
|
||||
throw new ResponseErrorException(toResponseError());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server.rename
|
||||
|
||||
import java.util.List
|
||||
import org.apache.log4j.Logger
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity
|
||||
import org.eclipse.xtext.util.ITextRegion
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
class ServerRefactoringIssueAcceptor implements RefactoringIssueAcceptor {
|
||||
|
||||
static class Issue {
|
||||
Severity severity
|
||||
String message
|
||||
}
|
||||
|
||||
List<Issue> issues = newArrayList
|
||||
|
||||
override add(Severity severity, String message, URI uri, ResourceSet resourceSet) {
|
||||
addIssue(severity, message)
|
||||
}
|
||||
|
||||
override add(Severity severity, String message, URI resourceUri) {
|
||||
addIssue(severity, message)
|
||||
}
|
||||
|
||||
override add(Severity severity, String message, EObject element) {
|
||||
addIssue(severity, message)
|
||||
}
|
||||
|
||||
override add(Severity severity, String message, EObject element, ITextRegion region) {
|
||||
addIssue(severity, message)
|
||||
}
|
||||
|
||||
override add(Severity severity, String message, Exception exc, Logger log) {
|
||||
addIssue(severity, message)
|
||||
}
|
||||
|
||||
override add(Severity severity, String message, Object... params) {
|
||||
issues += new Issue => [
|
||||
it.severity = severity
|
||||
it.message = message
|
||||
]
|
||||
}
|
||||
|
||||
protected def boolean addIssue(Severity severity, String message) {
|
||||
issues += new Issue => [
|
||||
it.severity = severity
|
||||
it.message = message
|
||||
]
|
||||
}
|
||||
|
||||
def Severity getMaximumSeverity() {
|
||||
if (issues.size > 0 )
|
||||
issues.minBy[severity]?.severity
|
||||
else
|
||||
Severity.OK
|
||||
}
|
||||
|
||||
def ResponseError toResponseError() {
|
||||
val maxSeverity = maximumSeverity
|
||||
new ResponseError => [
|
||||
message = switch (maxSeverity) {
|
||||
case OK: 'Refactoring is possible'
|
||||
case INFO: 'Refactoring is possible'
|
||||
case WARNING: 'Refactoring could cause issues'
|
||||
case ERROR: 'Refactoring has errors'
|
||||
case FATAL: 'Refactoring cannot be performed'
|
||||
}
|
||||
data = issues.sortBy[severity].reverse.map[message].join('\n')
|
||||
code = switch (maxSeverity) {
|
||||
case OK: 0
|
||||
case INFO: 0
|
||||
case WARNING: 0
|
||||
case ERROR: ResponseErrorCode.UnknownErrorCode.value
|
||||
case FATAL: ResponseErrorCode.UnknownErrorCode.value
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def checkSeverity() {
|
||||
if(maximumSeverity < Severity.WARNING)
|
||||
throw new ResponseErrorException(toResponseError)
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.eclipse.xtext.util.DisposableRegistry;
|
||||
import org.eclipse.xtext.util.IDisposable;
|
||||
|
||||
/**
|
||||
* Provider for executor services. By calling {@link #dispose()} all created executor services are shut down.
|
||||
* <p>
|
||||
* In some situations it is necessary to use multiple instances of executor services in order to avoid deadlocks.
|
||||
* That can be achieved with the {@link #get(String)} method, which will return a different instance for each key.
|
||||
*/
|
||||
@Singleton
|
||||
@SuppressWarnings("all")
|
||||
public class ExecutorServiceProvider implements Provider<ExecutorService>, IDisposable {
|
||||
@Inject
|
||||
public void registerTo(final DisposableRegistry disposableRegistry) {
|
||||
disposableRegistry.register(this);
|
||||
}
|
||||
|
||||
private final Map<String, ExecutorService> instanceCache = Maps.<String, ExecutorService>newHashMapWithExpectedSize(3);
|
||||
|
||||
@Override
|
||||
public ExecutorService get() {
|
||||
return this.get(null);
|
||||
}
|
||||
|
||||
public ExecutorService get(final String key) {
|
||||
ExecutorService result = this.instanceCache.get(key);
|
||||
if ((result == null)) {
|
||||
synchronized (this.instanceCache) {
|
||||
result = this.instanceCache.get(key);
|
||||
if ((result == null)) {
|
||||
result = this.createInstance(key);
|
||||
this.instanceCache.put(key, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected ExecutorService createInstance(final String key) {
|
||||
return Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
Collection<ExecutorService> _values = this.instanceCache.values();
|
||||
for (final ExecutorService executorService : _values) {
|
||||
executorService.shutdown();
|
||||
}
|
||||
this.instanceCache.clear();
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import org.eclipse.xtend.lib.annotations.Data;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class AlternativeImageDescription implements IImageDescription {
|
||||
private final List<String> imageIDs;
|
||||
|
||||
public AlternativeImageDescription(final Iterable<String> imageIDs) {
|
||||
this.imageIDs = ImmutableList.<String>copyOf(imageIDs);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
return 31 * 1 + ((this.imageIDs== null) ? 0 : this.imageIDs.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AlternativeImageDescription other = (AlternativeImageDescription) obj;
|
||||
if (this.imageIDs == null) {
|
||||
if (other.imageIDs != null)
|
||||
return false;
|
||||
} else if (!this.imageIDs.equals(other.imageIDs))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("imageIDs", this.imageIDs);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<String> getImageIDs() {
|
||||
return this.imageIDs;
|
||||
}
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import org.eclipse.xtend.lib.annotations.Data;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class DecoratedImageDescription implements IImageDescription {
|
||||
private final IImageDescription baseImage;
|
||||
|
||||
private final List<IImageDescription> decorators;
|
||||
|
||||
public DecoratedImageDescription(final IImageDescription baseImage, final IImageDescription... decorators) {
|
||||
this.baseImage = baseImage;
|
||||
this.decorators = ImmutableList.<IImageDescription>copyOf(decorators);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.baseImage== null) ? 0 : this.baseImage.hashCode());
|
||||
return prime * result + ((this.decorators== null) ? 0 : this.decorators.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
DecoratedImageDescription other = (DecoratedImageDescription) obj;
|
||||
if (this.baseImage == null) {
|
||||
if (other.baseImage != null)
|
||||
return false;
|
||||
} else if (!this.baseImage.equals(other.baseImage))
|
||||
return false;
|
||||
if (this.decorators == null) {
|
||||
if (other.decorators != null)
|
||||
return false;
|
||||
} else if (!this.decorators.equals(other.decorators))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("baseImage", this.baseImage);
|
||||
b.add("decorators", this.decorators);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IImageDescription getBaseImage() {
|
||||
return this.baseImage;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<IImageDescription> getDecorators() {
|
||||
return this.decorators;
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.ide.labels.AlternativeImageDescription;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescriptionProvider;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class EClassImageDescriptionProvider implements IImageDescriptionProvider {
|
||||
@Override
|
||||
public IImageDescription getImageDescription(final Object element) {
|
||||
IImageDescription _switchResult = null;
|
||||
boolean _matched = false;
|
||||
if (element instanceof EClass) {
|
||||
_matched=true;
|
||||
String _name = ((EClass)element).getName();
|
||||
final Function1<EClass, String> _function = (EClass it) -> {
|
||||
return it.getName();
|
||||
};
|
||||
List<String> _map = ListExtensions.<EClass, String>map(((EClass)element).getEAllSuperTypes(), _function);
|
||||
Iterable<String> _plus = Iterables.<String>concat(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList(_name)), _map);
|
||||
String _imageID = IImageDescription.DEFAULT.getImageID();
|
||||
Iterable<String> _plus_1 = Iterables.<String>concat(_plus, Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList(_imageID)));
|
||||
_switchResult = new AlternativeImageDescription(_plus_1);
|
||||
}
|
||||
if (!_matched) {
|
||||
if (element instanceof EObject) {
|
||||
_matched=true;
|
||||
_switchResult = this.getImageDescription(((EObject)element).eClass());
|
||||
}
|
||||
}
|
||||
if (!_matched) {
|
||||
if (element instanceof IEObjectDescription) {
|
||||
_matched=true;
|
||||
_switchResult = this.getImageDescription(((IEObjectDescription)element).getEClass());
|
||||
}
|
||||
}
|
||||
if (!_matched) {
|
||||
_switchResult = IImageDescription.DEFAULT;
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import org.eclipse.xtend.lib.annotations.Data;
|
||||
import org.eclipse.xtext.ide.labels.IImageDescription;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class SimpleImageDescription implements IImageDescription {
|
||||
private final String imageID;
|
||||
|
||||
public SimpleImageDescription(final String imageID) {
|
||||
super();
|
||||
this.imageID = imageID;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
return 31 * 1 + ((this.imageID== null) ? 0 : this.imageID.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SimpleImageDescription other = (SimpleImageDescription) obj;
|
||||
if (this.imageID == null) {
|
||||
if (other.imageID != null)
|
||||
return false;
|
||||
} else if (!this.imageID.equals(other.imageID))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("imageID", this.imageID);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getImageID() {
|
||||
return this.imageID;
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.labels;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.ide.labels.INameLabelProvider;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
import org.eclipse.xtext.util.SimpleAttributeResolver;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class SimpleNameLabelProvider implements INameLabelProvider {
|
||||
@Override
|
||||
public String getNameLabel(final Object element) {
|
||||
String _switchResult = null;
|
||||
boolean _matched = false;
|
||||
if (element instanceof EObject) {
|
||||
_matched=true;
|
||||
_switchResult = SimpleAttributeResolver.NAME_RESOLVER.apply(((EObject)element));
|
||||
}
|
||||
if (!_matched) {
|
||||
if (element instanceof IEObjectDescription) {
|
||||
_matched=true;
|
||||
_switchResult = ((IEObjectDescription)element).getName().getLastSegment();
|
||||
}
|
||||
}
|
||||
if (!_matched) {
|
||||
_switchResult = null;
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import com.google.inject.ImplementedBy;
|
||||
import com.google.inject.Inject;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.conversion.IValueConverterService;
|
||||
import org.eclipse.xtext.conversion.ValueConverterException;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@ImplementedBy(IRenameNameValidator.RuleBasedNameValidator.class)
|
||||
@SuppressWarnings("all")
|
||||
public interface IRenameNameValidator {
|
||||
class RuleBasedNameValidator implements IRenameNameValidator {
|
||||
@Inject
|
||||
private IValueConverterService valueConverterService;
|
||||
|
||||
@Override
|
||||
public void validate(final EObject target, final String newName, final RefactoringIssueAcceptor issues) {
|
||||
try {
|
||||
this.getNameAsValue(newName);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof ValueConverterException) {
|
||||
final ValueConverterException e = (ValueConverterException)_t;
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("Illegal name: ");
|
||||
String _message = e.getMessage();
|
||||
_builder.append(_message);
|
||||
issues.add(RefactoringIssueAcceptor.Severity.FATAL, _builder.toString());
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String getNameAsText(final String nameAsValue) {
|
||||
return this.valueConverterService.toString(nameAsValue, this.getRuleName());
|
||||
}
|
||||
|
||||
protected String getNameAsValue(final String nameAsText) {
|
||||
return this.valueConverterService.toValue(nameAsText, this.getRuleName(), null).toString();
|
||||
}
|
||||
|
||||
protected String getRuleName() {
|
||||
return "ID";
|
||||
}
|
||||
}
|
||||
|
||||
void validate(final EObject target, final String newName, final RefactoringIssueAcceptor issues);
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.emf.ecore.EAttribute;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.ide.refactoring.RenameChange;
|
||||
import org.eclipse.xtext.ide.refactoring.RenameContext;
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
|
||||
/**
|
||||
* Called to rename an element in the {@link IChangeSerializer} based refactoring.
|
||||
*
|
||||
* Clients may extend the {@link DefaultImpl} to customize the behavior or implement
|
||||
* this interface directly.
|
||||
*
|
||||
* Changes are usually performed in the The {@link RenameContext}
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface IRenameStrategy2 {
|
||||
class DefaultImpl implements IRenameStrategy2 {
|
||||
@Inject
|
||||
private IResourceServiceProvider resourceServiceProvider;
|
||||
|
||||
public boolean canHandle(final RenameChange change) {
|
||||
return this.resourceServiceProvider.canHandle(change.getTargetURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyRename(final RenameContext context) {
|
||||
final Function1<RenameChange, Boolean> _function = (RenameChange it) -> {
|
||||
return Boolean.valueOf(this.canHandle(it));
|
||||
};
|
||||
final Consumer<RenameChange> _function_1 = (RenameChange change) -> {
|
||||
final IChangeSerializer.IModification<EObject> _function_2 = (EObject it) -> {
|
||||
this.doRename(it, change, context);
|
||||
};
|
||||
context.addModification(change, _function_2);
|
||||
};
|
||||
IterableExtensions.filter(context.getChanges(), _function).forEach(_function_1);
|
||||
}
|
||||
|
||||
protected void doRename(final EObject target, final RenameChange change, final RenameContext context) {
|
||||
final EAttribute nameAttribute = this.getNameEAttribute(target);
|
||||
if ((nameAttribute != null)) {
|
||||
target.eSet(nameAttribute, change.getNewName());
|
||||
} else {
|
||||
String _name = target.eClass().getName();
|
||||
String _plus = ("Element of class " + _name);
|
||||
String _plus_1 = (_plus + " cannot be renamed.");
|
||||
context.getIssues().add(RefactoringIssueAcceptor.Severity.WARNING, _plus_1);
|
||||
}
|
||||
}
|
||||
|
||||
protected EAttribute getNameEAttribute(final EObject target) {
|
||||
final Function1<EAttribute, Boolean> _function = (EAttribute it) -> {
|
||||
return Boolean.valueOf((Objects.equal(it.getName(), "name") && Objects.equal(it.getEType(), EcorePackage.Literals.ESTRING)));
|
||||
};
|
||||
return IterableExtensions.<EAttribute>head(IterableExtensions.<EAttribute>filter(target.eClass().getEAllAttributes(), _function));
|
||||
}
|
||||
}
|
||||
|
||||
void applyRename(final RenameContext context);
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext;
|
||||
|
||||
/**
|
||||
* Allows a language to execute side-effects when the URI of a resource changes.
|
||||
*
|
||||
* Such changes can be move, rename and copy operations, e.g. triggered by the
|
||||
* user in a file browser. An example for a language in which such side-effects
|
||||
* would make sense is Java, where the package name and the name of the first
|
||||
* public top-level class must match the resource's path.
|
||||
*
|
||||
* Clients usually call {@link ResourceRelocationContext#addModification} to
|
||||
* register their side effects. This way it is ensured that the resource is
|
||||
* properly loaded and watched for changes.
|
||||
*
|
||||
* In Eclipse, {@link IResourceRelocationStrategy} are registered to an extension
|
||||
* point.
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface IResourceRelocationStrategy {
|
||||
void applyChange(final ResourceRelocationContext context);
|
||||
|
||||
/**
|
||||
* By overriding this method client implementations may explicitly demand for relying
|
||||
* on the persisted index during a refactoring.
|
||||
*
|
||||
* Refactoring operations usually rely on a lively created resource index. However,
|
||||
* in case of refactorings affecting a large amounts of files live resource indexing
|
||||
* may lead to heap pollution and resultant performance decreases.
|
||||
*
|
||||
* If a persisted index is supported by the runtime environment and if at least one of the
|
||||
* registered implementations of {@link IResourceRelocationStrategy} demands for relying
|
||||
* on the persisted index and if, furthermore, required preconditions are satisfied,
|
||||
* like no open editor is dirty and the persisted index is up-to-date, the refactoring
|
||||
* operation will rely entirely on the persisted index for, e.g., determining incoming
|
||||
* references of affected objects, and skip the creation of a live resource index.
|
||||
*
|
||||
* If some of the additional required preconditions are not met the runtime may execute
|
||||
* actions to make the conditions satisfied or reject the refactoring.
|
||||
*
|
||||
* @since 2.18
|
||||
*/
|
||||
default boolean requiresUsageOfPersistedIndex(final ResourceRelocationContext context) {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2018 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface RefactoringIssueAcceptor {
|
||||
enum Severity {
|
||||
FATAL,
|
||||
|
||||
ERROR,
|
||||
|
||||
WARNING,
|
||||
|
||||
INFO,
|
||||
|
||||
OK;
|
||||
}
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final URI uri, final ResourceSet resourceSet);
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final URI resourceUri);
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final EObject element);
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final EObject element, final ITextRegion region);
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final Exception exc, final Logger log);
|
||||
|
||||
void add(final RefactoringIssueAcceptor.Severity severity, final String message, final Object... params);
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtend.lib.annotations.Data;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class RenameChange {
|
||||
private final String newName;
|
||||
|
||||
private final URI targetURI;
|
||||
|
||||
public RenameChange(final String newName, final URI targetURI) {
|
||||
super();
|
||||
this.newName = newName;
|
||||
this.targetURI = targetURI;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.newName== null) ? 0 : this.newName.hashCode());
|
||||
return prime * result + ((this.targetURI== null) ? 0 : this.targetURI.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
RenameChange other = (RenameChange) obj;
|
||||
if (this.newName == null) {
|
||||
if (other.newName != null)
|
||||
return false;
|
||||
} else if (!this.newName.equals(other.newName))
|
||||
return false;
|
||||
if (this.targetURI == null) {
|
||||
if (other.targetURI != null)
|
||||
return false;
|
||||
} else if (!this.targetURI.equals(other.targetURI))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("newName", this.newName);
|
||||
b.add("targetURI", this.targetURI);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getNewName() {
|
||||
return this.newName;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public URI getTargetURI() {
|
||||
return this.targetURI;
|
||||
}
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import java.util.List;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.xtend.lib.annotations.AccessorType;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.ide.refactoring.RenameChange;
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@FinalFieldsConstructor
|
||||
@Accessors(AccessorType.PUBLIC_GETTER)
|
||||
@SuppressWarnings("all")
|
||||
public class RenameContext {
|
||||
private final List<? extends RenameChange> changes;
|
||||
|
||||
private final ResourceSet resourceSet;
|
||||
|
||||
private final IChangeSerializer changeSerializer;
|
||||
|
||||
private final RefactoringIssueAcceptor issues;
|
||||
|
||||
public void addModification(final RenameChange change, final IChangeSerializer.IModification<EObject> modification) {
|
||||
final EObject target = this.resourceSet.getEObject(change.getTargetURI(), true);
|
||||
if ((target != null)) {
|
||||
EcoreUtil.resolveAll(target.eResource());
|
||||
this.changeSerializer.<EObject>addModification(target, modification);
|
||||
} else {
|
||||
this.issues.add(RefactoringIssueAcceptor.Severity.ERROR, "Element cannot be found", change.getTargetURI());
|
||||
}
|
||||
}
|
||||
|
||||
public RenameContext(final List<? extends RenameChange> changes, final ResourceSet resourceSet, final IChangeSerializer changeSerializer, final RefactoringIssueAcceptor issues) {
|
||||
super();
|
||||
this.changes = changes;
|
||||
this.resourceSet = resourceSet;
|
||||
this.changeSerializer = changeSerializer;
|
||||
this.issues = issues;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<? extends RenameChange> getChanges() {
|
||||
return this.changes;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public ResourceSet getResourceSet() {
|
||||
return this.resourceSet;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IChangeSerializer getChangeSerializer() {
|
||||
return this.changeSerializer;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public RefactoringIssueAcceptor getIssues() {
|
||||
return this.issues;
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtend.lib.annotations.Data;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
||||
|
||||
/**
|
||||
* URIs can also refer to folders and non-Xtext resources.
|
||||
*
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class ResourceRelocationChange {
|
||||
private final URI fromURI;
|
||||
|
||||
private final URI toURI;
|
||||
|
||||
private final boolean isFile;
|
||||
|
||||
public ResourceRelocationChange(final URI fromURI, final URI toURI, final boolean isFile) {
|
||||
super();
|
||||
this.fromURI = fromURI;
|
||||
this.toURI = toURI;
|
||||
this.isFile = isFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.fromURI== null) ? 0 : this.fromURI.hashCode());
|
||||
result = prime * result + ((this.toURI== null) ? 0 : this.toURI.hashCode());
|
||||
return prime * result + (this.isFile ? 1231 : 1237);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ResourceRelocationChange other = (ResourceRelocationChange) obj;
|
||||
if (this.fromURI == null) {
|
||||
if (other.fromURI != null)
|
||||
return false;
|
||||
} else if (!this.fromURI.equals(other.fromURI))
|
||||
return false;
|
||||
if (this.toURI == null) {
|
||||
if (other.toURI != null)
|
||||
return false;
|
||||
} else if (!this.toURI.equals(other.toURI))
|
||||
return false;
|
||||
if (other.isFile != this.isFile)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public String toString() {
|
||||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("fromURI", this.fromURI);
|
||||
b.add("toURI", this.toURI);
|
||||
b.add("isFile", this.isFile);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Pure
|
||||
public URI getFromURI() {
|
||||
return this.fromURI;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public URI getToURI() {
|
||||
return this.toURI;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public boolean isFile() {
|
||||
return this.isFile;
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import java.util.List;
|
||||
import org.eclipse.xtext.resource.IResourceDescription;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface BuildListener {
|
||||
void afterBuild(final List<IResourceDescription.Delta> deltas);
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.lsp4j.Location;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.xtext.ide.server.UriExtensions;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.util.LineAndColumn;
|
||||
import org.eclipse.xtext.xbase.lib.Extension;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@SuppressWarnings("all")
|
||||
public class DocumentExtensions {
|
||||
@Inject
|
||||
@Extension
|
||||
private UriExtensions _uriExtensions;
|
||||
|
||||
@Inject
|
||||
private ILocationInFileProvider locationInFileProvider;
|
||||
|
||||
public Position newPosition(final Resource resource, final int offset) {
|
||||
if ((resource instanceof XtextResource)) {
|
||||
final ICompositeNode rootNode = ((XtextResource)resource).getParseResult().getRootNode();
|
||||
final LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(rootNode, offset);
|
||||
int _line = lineAndColumn.getLine();
|
||||
int _minus = (_line - 1);
|
||||
int _column = lineAndColumn.getColumn();
|
||||
int _minus_1 = (_column - 1);
|
||||
return new Position(_minus, _minus_1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Range newRange(final Resource resource, final int startOffset, final int endOffset) {
|
||||
final Position startPosition = this.newPosition(resource, startOffset);
|
||||
if ((startPosition == null)) {
|
||||
return null;
|
||||
}
|
||||
final Position endPosition = this.newPosition(resource, endOffset);
|
||||
if ((endPosition == null)) {
|
||||
return null;
|
||||
}
|
||||
return new Range(startPosition, endPosition);
|
||||
}
|
||||
|
||||
public Range newRange(final Resource resource, final ITextRegion region) {
|
||||
if ((region == null)) {
|
||||
return null;
|
||||
}
|
||||
int _offset = region.getOffset();
|
||||
int _offset_1 = region.getOffset();
|
||||
int _length = region.getLength();
|
||||
int _plus = (_offset_1 + _length);
|
||||
return this.newRange(resource, _offset, _plus);
|
||||
}
|
||||
|
||||
public Location newLocation(final Resource resource, final ITextRegion textRegion) {
|
||||
final Range range = this.newRange(resource, textRegion);
|
||||
if ((range == null)) {
|
||||
return null;
|
||||
}
|
||||
final String uri = this._uriExtensions.toUriString(resource.getURI());
|
||||
return new Location(uri, range);
|
||||
}
|
||||
|
||||
public Location newLocation(final EObject object) {
|
||||
final Resource resource = object.eResource();
|
||||
final ITextRegion textRegion = this.locationInFileProvider.getSignificantTextRegion(object);
|
||||
return this.newLocation(resource, textRegion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns with the {@link Location location} that represents the {@link ILocationInFileProvider#getFullTextRegion full text region}
|
||||
* of the argument.
|
||||
*
|
||||
* @since 2.16
|
||||
*/
|
||||
public Location newFullLocation(final EObject object) {
|
||||
final Resource resource = object.eResource();
|
||||
final ITextRegion textRegion = this.locationInFileProvider.getFullTextRegion(object);
|
||||
return this.newLocation(resource, textRegion);
|
||||
}
|
||||
|
||||
public Location newLocation(final EObject owner, final EStructuralFeature feature, final int indexInList) {
|
||||
final Resource resource = owner.eResource();
|
||||
final ITextRegion textRegion = this.locationInFileProvider.getSignificantTextRegion(owner, feature, indexInList);
|
||||
return this.newLocation(resource, textRegion);
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
/**
|
||||
* Normalizes file uris without authorities (<code>file:/path...</code>) to contain an empty authority (i.e. starts with three slashes:<code>file:///path...</code>).
|
||||
*
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@SuppressWarnings("all")
|
||||
public class UriExtensions extends org.eclipse.xtext.util.UriExtensions {
|
||||
/**
|
||||
* returns the string representation of the given URI (with empty authority, if absent and has file scheme).
|
||||
*/
|
||||
public String toUriString(final URI uri) {
|
||||
return this.withEmptyAuthority(uri).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a java.net.URI into a string representation with empty authority, if absent and has file scheme.
|
||||
*/
|
||||
public String toUriString(final java.net.URI uri) {
|
||||
return this.toUriString(URI.createURI(uri.normalize().toString()));
|
||||
}
|
||||
}
|
|
@ -1,133 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2018 itemis AG (http://www.itemis.com) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.formatting;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.eclipse.lsp4j.DocumentFormattingParams;
|
||||
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
|
||||
import org.eclipse.lsp4j.FormattingOptions;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.TextEdit;
|
||||
import org.eclipse.xtext.formatting.IIndentationInformation;
|
||||
import org.eclipse.xtext.formatting2.FormatterRequest;
|
||||
import org.eclipse.xtext.formatting2.IFormatter2;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.ITextReplacement;
|
||||
import org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.preferences.ITypedPreferenceValues;
|
||||
import org.eclipse.xtext.preferences.MapBasedPreferenceValues;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.util.TextRegion;
|
||||
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.ListExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
|
||||
|
||||
/**
|
||||
* Language Service Implementation for Formatting and Range-Formatting
|
||||
*
|
||||
* @author Christian Dietrich - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class FormattingService {
|
||||
@Inject(optional = true)
|
||||
private Provider<IFormatter2> formatter2Provider;
|
||||
|
||||
@Inject
|
||||
private Provider<FormatterRequest> formatterRequestProvider;
|
||||
|
||||
@Inject
|
||||
private TextRegionAccessBuilder regionBuilder;
|
||||
|
||||
@Inject
|
||||
private IIndentationInformation indentationInformation;
|
||||
|
||||
public List<? extends TextEdit> format(final Document document, final XtextResource resource, final DocumentFormattingParams params, final CancelIndicator cancelIndicator) {
|
||||
final int offset = 0;
|
||||
final int length = document.getContents().length();
|
||||
if (((length == 0) || resource.getContents().isEmpty())) {
|
||||
return CollectionLiterals.<TextEdit>emptyList();
|
||||
}
|
||||
return this.format(resource, document, offset, length, params.getOptions());
|
||||
}
|
||||
|
||||
public List<? extends TextEdit> format(final Document document, final XtextResource resource, final DocumentRangeFormattingParams params, final CancelIndicator cancelIndicator) {
|
||||
final int offset = document.getOffSet(params.getRange().getStart());
|
||||
int _offSet = document.getOffSet(params.getRange().getEnd());
|
||||
final int length = (_offSet - offset);
|
||||
return this.format(resource, document, offset, length, params.getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.14
|
||||
*/
|
||||
public List<TextEdit> format(final XtextResource resource, final Document document, final int offset, final int length, final FormattingOptions options) {
|
||||
String indent = this.indentationInformation.getIndentString();
|
||||
if ((options != null)) {
|
||||
boolean _isInsertSpaces = options.isInsertSpaces();
|
||||
if (_isInsertSpaces) {
|
||||
indent = Strings.padEnd("", options.getTabSize(), ' ');
|
||||
}
|
||||
}
|
||||
if ((this.formatter2Provider != null)) {
|
||||
final MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
|
||||
preferences.put("indentation", indent);
|
||||
TextRegion _textRegion = new TextRegion(offset, length);
|
||||
final List<ITextReplacement> replacements = this.format2(resource, _textRegion, preferences);
|
||||
final Function1<ITextReplacement, TextEdit> _function = (ITextReplacement r) -> {
|
||||
return this.toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength());
|
||||
};
|
||||
return IterableExtensions.<TextEdit>toList(ListExtensions.<ITextReplacement, TextEdit>map(replacements, _function));
|
||||
} else {
|
||||
return CollectionLiterals.<TextEdit>newArrayList();
|
||||
}
|
||||
}
|
||||
|
||||
protected TextEdit toTextEdit(final Document document, final String formattedText, final int startOffset, final int length) {
|
||||
TextEdit _textEdit = new TextEdit();
|
||||
final Procedure1<TextEdit> _function = (TextEdit it) -> {
|
||||
it.setNewText(formattedText);
|
||||
Range _range = new Range();
|
||||
final Procedure1<Range> _function_1 = (Range it_1) -> {
|
||||
it_1.setStart(document.getPosition(startOffset));
|
||||
it_1.setEnd(document.getPosition((startOffset + length)));
|
||||
};
|
||||
Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1);
|
||||
it.setRange(_doubleArrow);
|
||||
};
|
||||
return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function);
|
||||
}
|
||||
|
||||
protected List<ITextReplacement> format2(final XtextResource resource, final ITextRegion selection, final ITypedPreferenceValues preferences) {
|
||||
final FormatterRequest request = this.formatterRequestProvider.get();
|
||||
request.setAllowIdentityEdits(false);
|
||||
request.setFormatUndefinedHiddenRegionsOnly(false);
|
||||
if ((selection != null)) {
|
||||
request.setRegions(Collections.<ITextRegion>unmodifiableList(CollectionLiterals.<ITextRegion>newArrayList(selection)));
|
||||
}
|
||||
if ((preferences != null)) {
|
||||
request.setPreferences(preferences);
|
||||
}
|
||||
final ITextRegionAccess regionAccess = this.regionBuilder.forNodeModel(resource).create();
|
||||
request.setTextRegionAccess(regionAccess);
|
||||
final IFormatter2 formatter2 = this.formatter2Provider.get();
|
||||
final List<ITextReplacement> replacements = formatter2.format(request);
|
||||
return replacements;
|
||||
}
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.hover;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.HoverParams;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.ide.server.DocumentExtensions;
|
||||
import org.eclipse.xtext.ide.server.hover.HoverContext;
|
||||
import org.eclipse.xtext.ide.server.hover.IHoverService;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.eclipse.xtext.parser.IParseResult;
|
||||
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
|
||||
import org.eclipse.xtext.resource.ILocationInFileProvider;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.xbase.lib.Extension;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@Beta
|
||||
@SuppressWarnings("all")
|
||||
public class HoverService implements IHoverService {
|
||||
@Inject
|
||||
@Extension
|
||||
private DocumentExtensions _documentExtensions;
|
||||
|
||||
@Inject
|
||||
@Extension
|
||||
private EObjectAtOffsetHelper _eObjectAtOffsetHelper;
|
||||
|
||||
@Inject
|
||||
@Extension
|
||||
private ILocationInFileProvider _iLocationInFileProvider;
|
||||
|
||||
@Inject
|
||||
@Extension
|
||||
private IEObjectDocumentationProvider _iEObjectDocumentationProvider;
|
||||
|
||||
@Override
|
||||
public Hover hover(final Document document, final XtextResource resource, final HoverParams params, final CancelIndicator cancelIndicator) {
|
||||
final int offset = document.getOffSet(params.getPosition());
|
||||
final HoverContext context = this.createContext(document, resource, offset);
|
||||
return this.hover(context);
|
||||
}
|
||||
|
||||
protected HoverContext createContext(final Document document, final XtextResource resource, final int offset) {
|
||||
final EObject crossLinkedEObject = this._eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
|
||||
if ((crossLinkedEObject != null)) {
|
||||
boolean _eIsProxy = crossLinkedEObject.eIsProxy();
|
||||
if (_eIsProxy) {
|
||||
return null;
|
||||
}
|
||||
final IParseResult parseResult = resource.getParseResult();
|
||||
if ((parseResult == null)) {
|
||||
return null;
|
||||
}
|
||||
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
|
||||
if ((((leafNode != null) && leafNode.isHidden()) && (leafNode.getOffset() == offset))) {
|
||||
leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), (offset - 1));
|
||||
}
|
||||
if ((leafNode == null)) {
|
||||
return null;
|
||||
}
|
||||
final ITextRegion leafRegion = leafNode.getTextRegion();
|
||||
return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
|
||||
}
|
||||
final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(resource, offset);
|
||||
if ((element == null)) {
|
||||
return null;
|
||||
}
|
||||
final ITextRegion region = this._iLocationInFileProvider.getSignificantTextRegion(element);
|
||||
return new HoverContext(document, resource, offset, region, element);
|
||||
}
|
||||
|
||||
protected Hover hover(final HoverContext context) {
|
||||
if ((context == null)) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
final MarkupContent contents = this.getMarkupContent(context);
|
||||
if ((contents == null)) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
final Range range = this.getRange(context);
|
||||
if ((range == null)) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
return new Hover(contents, range);
|
||||
}
|
||||
|
||||
protected Range getRange(final HoverContext it) {
|
||||
boolean _contains = it.getRegion().contains(it.getOffset());
|
||||
boolean _not = (!_contains);
|
||||
if (_not) {
|
||||
return null;
|
||||
}
|
||||
return this._documentExtensions.newRange(it.getResource(), it.getRegion());
|
||||
}
|
||||
|
||||
protected MarkupContent getMarkupContent(final HoverContext it) {
|
||||
return this.toMarkupContent(this.getKind(it), this.getContents(it.getElement()));
|
||||
}
|
||||
|
||||
protected String getKind(final HoverContext it) {
|
||||
return "markdown";
|
||||
}
|
||||
|
||||
protected MarkupContent toMarkupContent(final String kind, final String value) {
|
||||
return new MarkupContent(kind, value);
|
||||
}
|
||||
|
||||
public String getContents(final EObject element) {
|
||||
if ((element == null)) {
|
||||
return "";
|
||||
}
|
||||
final String documentation = this._iEObjectDocumentationProvider.getDocumentation(element);
|
||||
if ((documentation == null)) {
|
||||
return "";
|
||||
}
|
||||
return documentation;
|
||||
}
|
||||
}
|
|
@ -1,181 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.server.rename;
|
||||
|
||||
import java.util.List;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
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.ListExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
* @since 2.13
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class ServerRefactoringIssueAcceptor implements RefactoringIssueAcceptor {
|
||||
public static class Issue {
|
||||
private RefactoringIssueAcceptor.Severity severity;
|
||||
|
||||
private String message;
|
||||
}
|
||||
|
||||
private List<ServerRefactoringIssueAcceptor.Issue> issues = CollectionLiterals.<ServerRefactoringIssueAcceptor.Issue>newArrayList();
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final URI uri, final ResourceSet resourceSet) {
|
||||
this.addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final URI resourceUri) {
|
||||
this.addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final EObject element) {
|
||||
this.addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final EObject element, final ITextRegion region) {
|
||||
this.addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final Exception exc, final Logger log) {
|
||||
this.addIssue(severity, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final RefactoringIssueAcceptor.Severity severity, final String message, final Object... params) {
|
||||
ServerRefactoringIssueAcceptor.Issue _issue = new ServerRefactoringIssueAcceptor.Issue();
|
||||
final Procedure1<ServerRefactoringIssueAcceptor.Issue> _function = (ServerRefactoringIssueAcceptor.Issue it) -> {
|
||||
it.severity = severity;
|
||||
it.message = message;
|
||||
};
|
||||
ServerRefactoringIssueAcceptor.Issue _doubleArrow = ObjectExtensions.<ServerRefactoringIssueAcceptor.Issue>operator_doubleArrow(_issue, _function);
|
||||
this.issues.add(_doubleArrow);
|
||||
}
|
||||
|
||||
protected boolean addIssue(final RefactoringIssueAcceptor.Severity severity, final String message) {
|
||||
ServerRefactoringIssueAcceptor.Issue _issue = new ServerRefactoringIssueAcceptor.Issue();
|
||||
final Procedure1<ServerRefactoringIssueAcceptor.Issue> _function = (ServerRefactoringIssueAcceptor.Issue it) -> {
|
||||
it.severity = severity;
|
||||
it.message = message;
|
||||
};
|
||||
ServerRefactoringIssueAcceptor.Issue _doubleArrow = ObjectExtensions.<ServerRefactoringIssueAcceptor.Issue>operator_doubleArrow(_issue, _function);
|
||||
return this.issues.add(_doubleArrow);
|
||||
}
|
||||
|
||||
public RefactoringIssueAcceptor.Severity getMaximumSeverity() {
|
||||
RefactoringIssueAcceptor.Severity _xifexpression = null;
|
||||
int _size = this.issues.size();
|
||||
boolean _greaterThan = (_size > 0);
|
||||
if (_greaterThan) {
|
||||
final Function1<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity> _function = (ServerRefactoringIssueAcceptor.Issue it) -> {
|
||||
return it.severity;
|
||||
};
|
||||
ServerRefactoringIssueAcceptor.Issue _minBy = IterableExtensions.<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity>minBy(this.issues, _function);
|
||||
RefactoringIssueAcceptor.Severity _severity = null;
|
||||
if (_minBy!=null) {
|
||||
_severity=_minBy.severity;
|
||||
}
|
||||
_xifexpression = _severity;
|
||||
} else {
|
||||
_xifexpression = RefactoringIssueAcceptor.Severity.OK;
|
||||
}
|
||||
return _xifexpression;
|
||||
}
|
||||
|
||||
public ResponseError toResponseError() {
|
||||
ResponseError _xblockexpression = null;
|
||||
{
|
||||
final RefactoringIssueAcceptor.Severity maxSeverity = this.getMaximumSeverity();
|
||||
ResponseError _responseError = new ResponseError();
|
||||
final Procedure1<ResponseError> _function = (ResponseError it) -> {
|
||||
String _switchResult = null;
|
||||
if (maxSeverity != null) {
|
||||
switch (maxSeverity) {
|
||||
case OK:
|
||||
_switchResult = "Refactoring is possible";
|
||||
break;
|
||||
case INFO:
|
||||
_switchResult = "Refactoring is possible";
|
||||
break;
|
||||
case WARNING:
|
||||
_switchResult = "Refactoring could cause issues";
|
||||
break;
|
||||
case ERROR:
|
||||
_switchResult = "Refactoring has errors";
|
||||
break;
|
||||
case FATAL:
|
||||
_switchResult = "Refactoring cannot be performed";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
it.setMessage(_switchResult);
|
||||
final Function1<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity> _function_1 = (ServerRefactoringIssueAcceptor.Issue it_1) -> {
|
||||
return it_1.severity;
|
||||
};
|
||||
final Function1<ServerRefactoringIssueAcceptor.Issue, String> _function_2 = (ServerRefactoringIssueAcceptor.Issue it_1) -> {
|
||||
return it_1.message;
|
||||
};
|
||||
it.setData(IterableExtensions.join(ListExtensions.<ServerRefactoringIssueAcceptor.Issue, String>map(ListExtensions.<ServerRefactoringIssueAcceptor.Issue>reverse(IterableExtensions.<ServerRefactoringIssueAcceptor.Issue, RefactoringIssueAcceptor.Severity>sortBy(this.issues, _function_1)), _function_2), "\n"));
|
||||
int _switchResult_1 = (int) 0;
|
||||
if (maxSeverity != null) {
|
||||
switch (maxSeverity) {
|
||||
case OK:
|
||||
_switchResult_1 = 0;
|
||||
break;
|
||||
case INFO:
|
||||
_switchResult_1 = 0;
|
||||
break;
|
||||
case WARNING:
|
||||
_switchResult_1 = 0;
|
||||
break;
|
||||
case ERROR:
|
||||
_switchResult_1 = ResponseErrorCode.UnknownErrorCode.getValue();
|
||||
break;
|
||||
case FATAL:
|
||||
_switchResult_1 = ResponseErrorCode.UnknownErrorCode.getValue();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
it.setCode(_switchResult_1);
|
||||
};
|
||||
_xblockexpression = ObjectExtensions.<ResponseError>operator_doubleArrow(_responseError, _function);
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
|
||||
public void checkSeverity() {
|
||||
RefactoringIssueAcceptor.Severity _maximumSeverity = this.getMaximumSeverity();
|
||||
boolean _lessThan = (_maximumSeverity.compareTo(RefactoringIssueAcceptor.Severity.WARNING) < 0);
|
||||
if (_lessThan) {
|
||||
ResponseError _responseError = this.toResponseError();
|
||||
throw new ResponseErrorException(_responseError);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue