org.eclipse.lsp4j.FoldingRange Java Examples

The following examples show how to use org.eclipse.lsp4j.FoldingRange. 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: XMLFoldingsTest.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
private static void assertRanges(String[] lines, ExpectedIndentRange[] expected, String message, Integer nRanges) {
	TextDocument document = new TextDocument(String.join("\n", lines), "test://foo/bar.json");
	DOMDocument xmlDocument = DOMParser.getInstance().parse(document, null);
	XMLLanguageService languageService = new XMLLanguageService();

	FoldingRangeCapabilities context = new FoldingRangeCapabilities();
	context.setRangeLimit(nRanges);
	XMLFoldingSettings settings = new XMLFoldingSettings();
	settings.setCapabilities(context);
	List<FoldingRange> actual = languageService.getFoldingRanges(xmlDocument, settings);

	List<ExpectedIndentRange> actualRanges = new ArrayList<>();
	for (FoldingRange f : actual) {
		actualRanges.add(r(f.getStartLine(), f.getEndLine(), f.getKind()));
	}
	Collections.sort(actualRanges, (r1, r2) -> r1.startLine - r2.startLine);
	assertArrayEquals(expected, actualRanges.toArray(), message);
}
 
Example #2
Source File: XMLTextDocumentService.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params) {
	return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
		return getXMLLanguageService().getFoldingRanges(xmlDocument, sharedSettings.getFoldingSettings(),
				cancelChecker);
	});
}
 
Example #3
Source File: JDTLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params) {
	logInfo(">> document/foldingRange");
	return computeAsyncWithClientProgress((monitor) -> {
		waitForLifecycleJobs(monitor);
		return new FoldingRangeHandler().foldingRange(params, monitor);
	});
}
 
Example #4
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<FoldingRange> foldingRange(FoldingRangeRequestParams params, IProgressMonitor monitor) {
	List<FoldingRange> $ = new ArrayList<>();
	ITypeRoot unit = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
	if (unit == null) {
		return $;
	}
	computeFoldingRanges($, unit, monitor);
	return $;
}
 
Example #5
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private List<FoldingRange> getFoldingRanges(String className) throws CoreException {
	String uri = ClassFileUtil.getURI(project, className);
	TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
	FoldingRangeRequestParams params = new FoldingRangeRequestParams();
	params.setTextDocument(identifier);
	return new FoldingRangeHandler().foldingRange(params, monitor);
}
 
Example #6
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void computeTypeRootRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IScanner scanner) throws CoreException {
	if (unit.hasChildren()) {
		for (IJavaElement child : unit.getChildren()) {
			if (child instanceof IImportContainer) {
				ISourceRange importRange = ((IImportContainer) child).getSourceRange();
				FoldingRange importFoldingRange = new FoldingRange(scanner.getLineNumber(importRange.getOffset()) - 1, scanner.getLineNumber(importRange.getOffset() + importRange.getLength()) - 1);
				importFoldingRange.setKind(FoldingRangeKind.Imports);
				foldingRanges.add(importFoldingRange);
			} else if (child instanceof IType) {
				computeTypeRanges(foldingRanges, (IType) child, scanner);
			}
		}
	}
}
 
Example #7
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void computeTypeRanges(List<FoldingRange> foldingRanges, IType unit, IScanner scanner) throws CoreException {
	ISourceRange typeRange = unit.getSourceRange();
	foldingRanges.add(new FoldingRange(scanner.getLineNumber(unit.getNameRange().getOffset()) - 1, scanner.getLineNumber(typeRange.getOffset() + typeRange.getLength()) - 1));
	IJavaElement[] children = unit.getChildren();
	for (IJavaElement c : children) {
		if (c instanceof IMethod) {
			computeMethodRanges(foldingRanges, (IMethod) c, scanner);
		} else if (c instanceof IType) {
			computeTypeRanges(foldingRanges, (IType) c, scanner);
		}
	}
}
 
Example #8
Source File: SyntaxLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params) {
	logInfo(">> document/foldingRange");
	return computeAsync((monitor) -> {
		waitForLifecycleJobs(monitor);
		return new FoldingRangeHandler().foldingRange(params, monitor);
	});
}
 
Example #9
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testTypes() throws Exception {
	String className = "org.sample.SimpleFoldingRange";
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	assertTrue(foldingRanges.size() == 7);
	assertHasFoldingRange(2, 3, FoldingRangeKind.Imports, foldingRanges);
	assertHasFoldingRange(5, 7, FoldingRangeKind.Comment, foldingRanges);
	assertHasFoldingRange(8, 24, null, foldingRanges);
	assertHasFoldingRange(10, 14, FoldingRangeKind.Comment, foldingRanges);
	assertHasFoldingRange(19, 23, null, foldingRanges);
	assertHasFoldingRange(20, 22, null, foldingRanges);
}
 
Example #10
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorTypes() throws Exception {
	String className = "org.sample.UnmatchFoldingRange";
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	assertTrue(foldingRanges.size() == 3);
	assertHasFoldingRange(2, 12, null, foldingRanges);
	assertHasFoldingRange(3, 10, null, foldingRanges);
	assertHasFoldingRange(5, 7, null, foldingRanges);
}
 
Example #11
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInvalidInput() throws Exception {
	String className = "org.sample.InvalidInputRange";
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	assertTrue(foldingRanges.size() == 3);
	assertHasFoldingRange(2, 4, "comment", foldingRanges);
	assertHasFoldingRange(5, 10, null, foldingRanges);
	assertHasFoldingRange(7, 9, null, foldingRanges);
}
 
Example #12
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRegionFoldingRanges() throws Exception {
	String className = "org.sample.RegionFoldingRange";
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	assertTrue(foldingRanges.size() == 7);
	assertHasFoldingRange(7, 15, FoldingRangeKind.Region, foldingRanges);
	assertHasFoldingRange(17, 23, FoldingRangeKind.Region, foldingRanges);
	assertHasFoldingRange(18, 20, FoldingRangeKind.Region, foldingRanges);
}
 
Example #13
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testStatementFoldingRanges() throws Exception {
	String className = "org.sample.StatementFoldingRange";
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	assertTrue(foldingRanges.size() == 18);
	assertHasFoldingRange(2, 4, FoldingRangeKind.Comment, foldingRanges);
	assertHasFoldingRange(5, 53, null, foldingRanges);
	assertHasFoldingRange(7, 52, null, foldingRanges);

	// First switch statement
	assertHasFoldingRange(10, 23, null, foldingRanges);
	assertHasFoldingRange(11, 18, null, foldingRanges);
	assertHasFoldingRange(19, 20, null, foldingRanges);
	assertHasFoldingRange(21, 22, null, foldingRanges);

	// Try catch:
	assertHasFoldingRange(12, 13, null, foldingRanges);
	assertHasFoldingRange(14, 16, null, foldingRanges);

	// If statement:
	assertHasFoldingRange(26, 27, null, foldingRanges);
	assertHasFoldingRange(28, 29, null, foldingRanges);
	assertHasFoldingRange(30, 32, null, foldingRanges);

	// Second switch statement:
	assertHasFoldingRange(36, 51, null, foldingRanges);
	assertHasFoldingRange(37, 40, null, foldingRanges);
	assertHasFoldingRange(41, 47, null, foldingRanges);
	assertHasFoldingRange(48, 50, null, foldingRanges);
}
 
Example #14
Source File: XMLFoldings.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
private static int addRange(FoldingRange range, List<FoldingRange> ranges) {
	ranges.add(range);
	return range.getStartLine();
}
 
Example #15
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void assertHasFoldingRange(int startLine, int endLine, String expectedKind, Collection<FoldingRange> foldingRanges) {
	Optional<FoldingRange> range = foldingRanges.stream().filter(s -> s.getStartLine() == startLine && s.getEndLine() == endLine).findFirst();
	assertTrue("Expected type" + expectedKind, range.get().getKind() == expectedKind);
}
 
Example #16
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isValid(FoldingRange range) {
	return range != null && range.getStartLine() <= range.getEndLine();
}
 
Example #17
Source File: FoldingRangeHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void testClass(String className) throws CoreException {
	List<FoldingRange> foldingRanges = getFoldingRanges(className);
	for (FoldingRange range : foldingRanges) {
		assertTrue("Class: " + className + ", FoldingRange:" + range.getKind() + " - invalid location.", isValid(range));
	}
}
 
Example #18
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IProgressMonitor monitor) {
	try {
		ISourceRange range = unit.getSourceRange();
		if (!SourceRange.isAvailable(range)) {
			return;
		}

		String contents = unit.getSource();
		if (StringUtils.isBlank(contents)) {
			return;
		}

		final int shift = range.getOffset();
		IScanner scanner = getScanner();
		scanner.setSource(contents.toCharArray());
		scanner.resetTo(shift, shift + range.getLength());

		int start = shift;
		int token = 0;
		Stack<Integer> regionStarts = new Stack<>();
		while (token != ITerminalSymbols.TokenNameEOF) {
			start = scanner.getCurrentTokenStartPosition();
			switch (token) {
				case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
				case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
					int end = scanner.getCurrentTokenEndPosition();
					FoldingRange commentFoldingRange = new FoldingRange(scanner.getLineNumber(start) - 1, scanner.getLineNumber(end) - 1);
					commentFoldingRange.setKind(FoldingRangeKind.Comment);
					foldingRanges.add(commentFoldingRange);
					break;
				case ITerminalSymbols.TokenNameCOMMENT_LINE:
					String currentSource = String.valueOf(scanner.getCurrentTokenSource());
					if (REGION_START_PATTERN.matcher(currentSource).lookingAt()) {
						regionStarts.push(start);
					} else if (REGION_END_PATTERN.matcher(currentSource).lookingAt()) {
						if (regionStarts.size() > 0) {
							FoldingRange regionFolding = new FoldingRange(scanner.getLineNumber(regionStarts.pop()) - 1, scanner.getLineNumber(start) - 1);
							regionFolding.setKind(FoldingRangeKind.Region);
							foldingRanges.add(regionFolding);
						}
					}
					break;
				default:
					break;
			}
			token = getNextToken(scanner);
		}
		computeTypeRootRanges(foldingRanges, unit, scanner);
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
		monitor.setCanceled(true);
	}
}
 
Example #19
Source File: XMLLanguageService.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public List<FoldingRange> getFoldingRanges(DOMDocument xmlDocument, XMLFoldingSettings context,
		CancelChecker cancelChecker) {
	return foldings.getFoldingRanges(xmlDocument.getTextDocument(), context, cancelChecker);
}
 
Example #20
Source File: XMLLanguageService.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public List<FoldingRange> getFoldingRanges(DOMDocument xmlDocument, XMLFoldingSettings context) {
	return getFoldingRanges(xmlDocument, context, NULL_CHECKER);
}
 
Example #21
Source File: TextDocumentService.java    From lsp4j with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The folding range request is sent from the client to the server to return all folding
 * ranges found in a given text document.
 * 
 * Since version 3.10.0
 */
@JsonRequest
default CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params) {
	throw new UnsupportedOperationException();
}