org.eclipse.xtext.util.Wrapper Java Examples

The following examples show how to use org.eclipse.xtext.util.Wrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaProjectSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public static IFolder createExternalFolder(String folderName) throws CoreException {
		final Wrapper<IFolder> wrapper = Wrapper.forType(IFolder.class);
		ResourcesPlugin.getWorkspace().run((monitor)->{
			IPath externalFolderPath = new Path(folderName);
			IProject externalFoldersProject = JavaModelManager.getExternalManager().getExternalFoldersProject();
			if (!externalFoldersProject.isAccessible()) {
				if (!externalFoldersProject.exists())
					externalFoldersProject.create(monitor());
				externalFoldersProject.open(monitor());
			}
			IFolder result = externalFoldersProject.getFolder(externalFolderPath);
			result.create(true, false, null);
//				JavaModelManager.getExternalManager().addFolder(result.getFullPath());
			wrapper.set(result);
		}, monitor());
		return wrapper.get();
	}
 
Example #2
Source File: Bug419429Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void replaceAndReparse(String model, int offset, int length, String change, String expectedReparseRegion)
		throws Exception {
	IParseResult parseResult = getParseResultAndExpect(model, UNKNOWN_EXPECTATION);
	PartialParsingPointers parsingPointers = getPartialParser().calculatePartialParsingPointers(parseResult, offset,
			length);
	String reparseRegion = getPartialParser().insertChangeIntoReplaceRegion(parsingPointers
			.getDefaultReplaceRootNode(), new ReplaceRegion(offset, length, change));
	assertEquals(expectedReparseRegion, reparseRegion);
	final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE);
	getPartialParser().setUnloader(new IReferableElementsUnloader() {
		@Override
		public void unloadRoot(EObject root) {
			unloaded.set(Boolean.TRUE);
		}
	});
	IParseResult partiallyReparse = reparse(parseResult, offset, length, change);
	assertTrue("unloaded", unloaded.get());
	String expectedReparseModel = model.substring(0, offset) + change + model.substring(offset + length);
	assertEquals(expectedReparseModel, partiallyReparse.getRootNode().getText());
	
	compareWithFullParse(model, offset, length, change);
}
 
Example #3
Source File: AbstractPartialParserReplaceTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void replaceAndReparse(String model, int offset, int length, String change, String expectedReparseRegion)
		throws Exception {
	IParseResult parseResult = getParseResult(model);
	PartialParsingPointers parsingPointers = getPartialParser().calculatePartialParsingPointers(parseResult, offset,
			length);
	String reparseRegion = getPartialParser().insertChangeIntoReplaceRegion(parsingPointers
			.getDefaultReplaceRootNode(), new ReplaceRegion(offset, length, change));
	assertEquals(expectedReparseRegion, reparseRegion);
	final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE);
	getPartialParser().setUnloader(new IReferableElementsUnloader() {
		@Override
		public void unloadRoot(EObject root) {
			unloaded.set(Boolean.TRUE);
		}
	});
	IParseResult partiallyReparse = reparse(parseResult, offset, length, change);
	assertTrue("unloaded", unloaded.get());
	assertFalse(partiallyReparse.getRootNode().getText(), partiallyReparse.hasSyntaxErrors());
	String expectedReparseModel = model.substring(0, offset) + change + model.substring(offset + length);
	assertEquals(expectedReparseModel, partiallyReparse.getRootNode().getText());
}
 
Example #4
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedList() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo");
	toMerge.add("bar");
	toMerge.add("baz");

	assertEquals("bar,foo,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList("bar", toMerge, Wrapper.wrap(false), NL)));
	assertEquals("bar,x,foo,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList("bar ,    x", toMerge, Wrapper.wrap(false), NL)));
	assertEquals("baz,bar,foo", removeWS(MergeableManifest.mergeIntoCommaSeparatedList("baz,bar,foo", toMerge, Wrapper.wrap(false), NL)));
	assertEquals("foo,bar,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList("", toMerge, Wrapper.wrap(false), NL)));
	assertEquals("foo,bar,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(null, toMerge, Wrapper.wrap(false), NL)));
}
 
Example #5
Source File: DefaultHierarchyNode.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isRecursive() {
	if (recursive == null) {
		recursive = Wrapper.wrap(internalIsRecursive());
	}
	return recursive.get().booleanValue();
}
 
Example #6
Source File: XtextResourceTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUnloadReferables() throws Exception {
	resource.reparse(simpleModel);
	final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE);
	resource.setUnloader(new IReferableElementsUnloader() {
		@Override
		public void unloadRoot(EObject root) {
			unloaded.set(Boolean.TRUE);
		}
	});
	resource.reparse(simpleModel);
	assertTrue("unloaded", unloaded.get());
}
 
Example #7
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedListWithCommaSeparatedParams() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo");
	toMerge.add("bar");
	toMerge.add("baz");
	
	assertEquals("bar;x-friends=\"xxx,foo,bar,zzz\",foo,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"bar;x-friends=\"xxx,foo,bar,zzz\"", toMerge, Wrapper.wrap(false), NL)));
}
 
Example #8
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedListWithParams() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo");
	toMerge.add("bar");
	toMerge.add("baz");

	assertEquals("bar;version=\"0.7.0\",foo,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"bar;version=\"0.7.0\"", toMerge, Wrapper.wrap(false), NL)));
	assertEquals("bar;special=foo,x,foo,baz", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"bar;special=foo ,    x", toMerge, Wrapper.wrap(false), NL)));
}
 
Example #9
Source File: ResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> getDeclaratorParameterMapping() {
	final Wrapper<Map<JvmTypeParameter, LightweightMergedBoundTypeArgument>> receiverTypeParameterMapping = Wrapper.wrap(Collections.<JvmTypeParameter, LightweightMergedBoundTypeArgument>emptyMap());
	XExpression receiver = getReceiver();
	if (receiver != null) {
		LightweightTypeReference receiverType = getReceiverType();
		if (receiverType == null) {
			throw new IllegalStateException("Cannot determine type of receiver "+ getReceiver());
		}
		JvmIdentifiableElement feature = getFeature();
		if (feature instanceof JvmFeature) {
			JvmDeclaredType declaringType = ((JvmFeature) feature).getDeclaringType();
			final LightweightTypeReference declaringTypeReference = receiverType.getOwner().newParameterizedTypeReference(declaringType);
			final TypeConformanceComputationArgument rawConformanceCheck = new TypeConformanceComputationArgument(true, false, false, false, false, false);
			if (declaringTypeReference.isAssignableFrom(receiverType, rawConformanceCheck)) {
				receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(receiverType));
			} else {
				CommonTypeComputationServices services = receiverType.getOwner().getServices();
				SynonymTypesProvider synonymProvider = services.getSynonymTypesProvider();
				synonymProvider.collectSynonymTypes(receiverType, new SynonymTypesProvider.Acceptor() {
					
					@Override
					protected boolean accept(LightweightTypeReference synonym, int hints) {
						if (declaringTypeReference.isAssignableFrom(synonym, rawConformanceCheck)) {
							receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(synonym));
							return false;
						}
						return true;
					}
				});
			}
			
		} else {
			receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(receiverType));
		}
	}
	return receiverTypeParameterMapping.get();
}
 
Example #10
Source File: NfaGraphFormatter.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected <STATE> Node createNodes(Nfa<STATE> nfa, STATE state, Map<STATE, Integer> names,
		Wrapper<Integer> lastName) {
	Integer name = names.get(state);
	if (name != null)
		return new Node(String.valueOf(name), true);
	lastName.set(lastName.get() + 1);
	names.put(state, lastName.get());
	Node node = new Node(String.valueOf(lastName.get()), false);
	for (STATE follower : nfa.getFollowers(state))
		node.children.add(createNodes(nfa, follower, names, lastName));
	return node;
}
 
Example #11
Source File: NfaGraphFormatter.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public <STATE> String format(Nfa<STATE> nfa) {
	Map<STATE, Integer> names = Maps.newLinkedHashMap();
	List<Node> nodes = Lists.newArrayList();
	nodes.add(createNodes(nfa, nfa.getStart(), names, new Wrapper<Integer>(0)));
	StringBuilder result = new StringBuilder();
	STATE starts = nfa.getStart();
	STATE stops = nfa.getStop();
	for (Map.Entry<STATE, Integer> e : names.entrySet()) {
		result.append(e.getValue() + ": " + e.getKey());
		if (starts == e.getKey())
			result.append(" (start)");
		if (stops == e.getKey())
			result.append(" (stop)");
		if (!new NfaUtil().canReachFinalState(nfa, e.getKey()))
			result.append(" (no connection to final state!)");
		result.append("\n");
	}
	Canvas canvas = new Canvas();
	int width = 0;
	for (Node n : nodes) {
		n.draw(canvas, 0, width, n.getMinWidth());
		width += n.getMinWidth();
	}
	//				result.append(n.getNameLine() + " ");
	//			result.append("\n");
	//			for (Node n : nodes)
	//				result.append(n.getBranchLine() + " ");
	//			result.append("\n");
	//			List<Node> children = Lists.newArrayList();
	//			for (Node n : nodes)
	//				children.addAll(n.children);
	//			nodes = children;
	//		}
	result.append(canvas);
	return result.toString();

}
 
Example #12
Source File: FragmentFakingEcoreResource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public FragmentFakingEcoreResource(URI uri, Wrapper<Boolean> isSaving) {
	super(uri);
	this.isSaving = isSaving;
	this.encoding = "UTF-8";
	this.getDefaultSaveOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, true);
	this.getDefaultSaveOptions().put(XMLResource.OPTION_LINE_WIDTH, 80);
	getDefaultSaveOptions().put(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware());
}
 
Example #13
Source File: HoverService.java    From xtext-web with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Compute a hover result for a content assist proposal at the given offset.
 */
public HoverResult getHover(XtextWebDocumentAccess document, String proposal, ITextRegion selection, int offset)
		throws InvalidRequestException {
	return document.readOnly(new CancelableUnitOfWork<HoverResult, IXtextWebDocument>() {
		@Override
		public HoverResult exec(IXtextWebDocument it, CancelIndicator cancelIndicator) throws Exception {
			ContentAssistContext[] contexts = contentAssistService.getContexts(it, selection, offset);
			Wrapper<Object> proposedElement = new Wrapper<Object>();
			Collection<ContentAssistContext> contextsList = Arrays.asList(contexts);
			contentAssistService.getProposalProvider().createProposals(contextsList,
					new IIdeContentProposalAcceptor() {
						@Override
						public void accept(ContentAssistEntry entry, int priority) {
							operationCanceledManager.checkCanceled(cancelIndicator);
							if (entry != null && entry.getSource() != null
									&& Objects.equal(entry.getProposal(), proposal)) {
								proposedElement.set(entry.getSource());
							}
						}

						@Override
						public boolean canAcceptMoreProposals() {
							return proposedElement.get() == null;
						}
					});
			return createHover(proposedElement.get(), it.getStateId(), cancelIndicator);
		}
	});
}
 
Example #14
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected <T extends Object> void genericTestInitializeSafely(final Function3<? super EObject, ? super String, ? super Procedure1<? super T>, ? extends EObject> create) {
  final XNullLiteral expr = XbaseFactory.eINSTANCE.createXNullLiteral();
  final Wrapper<Boolean> initialized = new Wrapper<Boolean>(Boolean.valueOf(false));
  final Procedure1<T> _function = (T it) -> {
    initialized.set(Boolean.valueOf(true));
    throw new RuntimeException();
  };
  final EObject element = create.apply(expr, "foo", _function);
  Assert.assertTrue((initialized.get()).booleanValue());
  Assert.assertNotNull(element);
}
 
Example #15
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void writeGrammar() {
  final Wrapper<Boolean> isSaving = Wrapper.<Boolean>wrap(Boolean.valueOf(false));
  final ResourceSet cloneInto = new ResourceSetImpl();
  Map<String, Object> _extensionToFactoryMap = cloneInto.getResourceFactoryRegistry().getExtensionToFactoryMap();
  FragmentFakingEcoreResource.FactoryImpl _factoryImpl = new FragmentFakingEcoreResource.FactoryImpl(isSaving);
  _extensionToFactoryMap.put(
    FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX, _factoryImpl);
  final ResourceSet resourceSet = EcoreUtil2.<ResourceSet>clone(cloneInto, this.getLanguage().getGrammar().eResource().getResourceSet());
  EObject _head = IterableExtensions.<EObject>head(resourceSet.getResource(this.getLanguage().getGrammar().eResource().getURI(), true).getContents());
  final Grammar copy = ((Grammar) _head);
  String _xifexpression = null;
  if ((this.xmlVersion == null)) {
    _xifexpression = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
  } else {
    String _xblockexpression = null;
    {
      String _simpleName = GrammarAccessFragment2.class.getSimpleName();
      String _plus = ("The property \'xmlVersion\' has been specified for this " + _simpleName);
      String _plus_1 = (_plus + ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
      GrammarAccessFragment2.LOG.warn(_plus_1);
      _xblockexpression = GrammarUtil.getClasspathRelativePathToXmi(copy);
    }
    _xifexpression = _xblockexpression;
  }
  final String path = _xifexpression;
  final URI uri = this.getProjectConfig().getRuntime().getSrcGen().getURI(path);
  final Resource resource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
  HashSet<Grammar> _hashSet = new HashSet<Grammar>();
  this.addAllGrammarsToResource(resource, copy, _hashSet);
  isSaving.set(Boolean.valueOf(true));
  final Map<String, Object> saveOptions = CollectionLiterals.<String, Object>newHashMap();
  if ((resource instanceof XMLResource)) {
    String _elvis = null;
    if (this.xmlVersion != null) {
      _elvis = this.xmlVersion;
    } else {
      _elvis = "1.0";
    }
    ((XMLResource)resource).setXMLVersion(_elvis);
  } else {
    if ((resource instanceof BinaryResourceImpl)) {
      saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
      saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.valueOf(true));
    }
  }
  try {
    resource.save(saveOptions);
  } catch (final Throwable _t) {
    if (_t instanceof IOException) {
      final IOException e = (IOException)_t;
      GrammarAccessFragment2.LOG.error(e.getMessage(), e);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  } finally {
    isSaving.set(Boolean.valueOf(false));
  }
}
 
Example #16
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedListValidParam() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo;foo=version");
	assertEquals("bar,foo;foo=version", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"bar", toMerge, Wrapper.wrap(false), NL)));
}
 
Example #17
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedListSkipWhenParamExists() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo;foo=other");
	assertEquals("foo;version=\"0.7.0\"", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"foo;version=\"0.7.0\"", toMerge, Wrapper.wrap(false), NL)));
}
 
Example #18
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testMergeIntoCommaSeparatedListAddParam() throws Exception {
	LinkedHashSet<String> toMerge = new LinkedHashSet<String>();
	toMerge.add("foo;version=\"0.7.0\"");
	assertEquals("foo;version=\"0.7.0\"", removeWS(MergeableManifest.mergeIntoCommaSeparatedList(
			"foo", toMerge, Wrapper.wrap(false), NL)));
}
 
Example #19
Source File: FragmentFakingEcoreResource.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public FactoryImpl(Wrapper<Boolean> isSaving) {
	this.isSaving = isSaving;
}