@Override public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) { logInfo(">> document/formatting"); FormatterHandler handler = new FormatterHandler(preferenceManager); return computeAsync((monitor) -> handler.formatting(params, monitor)); }
private static Map<String, String> getOptions(FormattingOptions options, ICompilationUnit cu) { Map<String, String> eclipseOptions = cu.getJavaProject().getOptions(true); Map<String, String> customOptions = options.entrySet().stream().filter(map -> chekIfValueIsNotNull(map.getValue())).collect(toMap(e -> e.getKey(), e -> getOptionValue(e.getValue()))); eclipseOptions.putAll(customOptions); Integer tabSize = options.getTabSize(); if (tabSize != null) { int tSize = tabSize.intValue(); if (tSize > 0) { eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(tSize)); } } boolean insertSpaces = options.isInsertSpaces(); eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB); return eclipseOptions; }
private static List<org.eclipse.lsp4j.TextEdit> convertEdits(TextEdit[] edits, IDocument document) { return Arrays.stream(edits).map(t -> convertEdit(t, document)).collect(Collectors.toList()); }
@Override public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) { logInfo(">> document/rangeFormatting"); FormatterHandler handler = new FormatterHandler(preferenceManager); return computeAsync((monitor) -> handler.rangeFormatting(params, monitor)); }
@Override public CompletableFuture<List<? extends TextEdit>> onTypeFormatting(DocumentOnTypeFormattingParams params) { logInfo(">> document/onTypeFormatting"); FormatterHandler handler = new FormatterHandler(preferenceManager); return computeAsync((monitor) -> handler.onTypeFormatting(params, monitor)); }
private List<org.eclipse.lsp4j.TextEdit> format(ICompilationUnit cu, IDocument document, IRegion region, FormattingOptions options, boolean includeComments, IProgressMonitor monitor) { if (cu == null || document == null || region == null || monitor.isCanceled()) { return Collections.emptyList(); } CodeFormatter formatter = ToolFactory.createCodeFormatter(getOptions(options, cu)); String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document); String sourceToFormat = document.get(); int kind = getFormattingKind(cu, includeComments); TextEdit format = formatter.format(kind, sourceToFormat, region.getOffset(), region.getLength(), 0, lineDelimiter); if (format == null || format.getChildren().length == 0 || monitor.isCanceled()) { // nothing to return return Collections.<org.eclipse.lsp4j.TextEdit>emptyList(); } MultiTextEdit flatEdit = TextEditUtil.flatten(format); return convertEdits(flatEdit.getChildren(), document); }
private List<? extends org.eclipse.lsp4j.TextEdit> format(String uri, FormattingOptions options, Position position, String triggerChar, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isJavaFormatOnTypeEnabled()) { return Collections.emptyList(); } ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); if (cu == null) { return Collections.emptyList(); } IRegion region = null; IDocument document = null; try { document = JsonRpcHelpers.toDocument(cu.getBuffer()); if (document != null && position != null) { region = getRegion(cu, document, position, triggerChar); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } if (region == null) { return Collections.emptyList(); } return format(cu, document, region, options, false, monitor); }
List<? extends org.eclipse.lsp4j.TextEdit> formatting(DocumentFormattingParams params, IProgressMonitor monitor) { return format(params.getTextDocument().getUri(), params.getOptions(), (Range) null, monitor); }
private static org.eclipse.lsp4j.TextEdit convertEdit(TextEdit edit, IDocument document) { org.eclipse.lsp4j.TextEdit textEdit = new org.eclipse.lsp4j.TextEdit(); if (edit instanceof ReplaceEdit) { ReplaceEdit replaceEdit = (ReplaceEdit) edit; textEdit.setNewText(replaceEdit.getText()); int offset = edit.getOffset(); textEdit.setRange(new Range(createPosition(document, offset), createPosition(document, offset + edit.getLength()))); } return textEdit; }
private List<org.eclipse.lsp4j.TextEdit> format(String uri, FormattingOptions options, Range range, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isJavaFormatEnabled()) { return Collections.emptyList(); } ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); if (cu == null) { return Collections.emptyList(); } IRegion region = null; IDocument document = null; try { document = JsonRpcHelpers.toDocument(cu.getBuffer()); if (document != null) { region = (range == null ? new Region(0, document.getLength()) : getRegion(range, document)); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } if (region == null) { return Collections.emptyList(); } return format(cu, document, region, options, preferenceManager.getPreferences().isJavaFormatComments(), monitor); }
List<? extends org.eclipse.lsp4j.TextEdit> rangeFormatting(DocumentRangeFormattingParams params, IProgressMonitor monitor) { return format(params.getTextDocument().getUri(), params.getOptions(), params.getRange(), monitor); }
public List<? extends org.eclipse.lsp4j.TextEdit> onTypeFormatting(DocumentOnTypeFormattingParams params, IProgressMonitor monitor) { return format(params.getTextDocument().getUri(), params.getOptions(), params.getPosition(), params.getCh(), monitor); }