|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + *******************************************************************************/ |
| 9 | +package org.eclipse.lsp4e.format; |
| 10 | + |
| 11 | +import org.eclipse.jdt.annotation.Nullable; |
| 12 | +import org.eclipse.jface.text.BadLocationException; |
| 13 | +import org.eclipse.jface.text.DocumentCommand; |
| 14 | +import org.eclipse.jface.text.IAutoEditStrategy; |
| 15 | +import org.eclipse.jface.text.IDocument; |
| 16 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 17 | +import org.eclipse.lsp4e.LanguageServerPlugin; |
| 18 | +import org.eclipse.lsp4e.LanguageServers; |
| 19 | +import org.eclipse.lsp4e.operations.format.LSPFormatter; |
| 20 | +import org.eclipse.lsp4j.DocumentOnTypeFormattingParams; |
| 21 | +import org.eclipse.lsp4j.FormattingOptions; |
| 22 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 23 | +import org.eclipse.lsp4j.jsonrpc.messages.Either; |
| 24 | + |
| 25 | +public class DocumentFormatOnTypeAutoEditStrategy implements IAutoEditStrategy { |
| 26 | + |
| 27 | + @Override |
| 28 | + public void customizeDocumentCommand(@Nullable IDocument document, @Nullable DocumentCommand command) { |
| 29 | + if (document == null || command == null || command.getCommandCount() > 1 || command.text.isEmpty()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + var executor = LanguageServers.forDocument(document) |
| 33 | + .withCapability(capabilities -> Either.forLeft(capabilities.getDocumentOnTypeFormattingProvider() != null |
| 34 | + && (command.text.equals(capabilities.getDocumentOnTypeFormattingProvider().getFirstTriggerCharacter()) || |
| 35 | + (capabilities.getDocumentOnTypeFormattingProvider().getMoreTriggerCharacter() != null && capabilities.getDocumentOnTypeFormattingProvider().getMoreTriggerCharacter().contains(command.text))))); |
| 36 | + if (!executor.anyMatching()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + FormattingOptions formattingOptions = LSPFormatter.getFormatOptions(); |
| 40 | + TextDocumentIdentifier textDocumentIdentifier = LSPEclipseUtils.toTextDocumentIdentifier(document); |
| 41 | + if (textDocumentIdentifier == null) { |
| 42 | + return; |
| 43 | + } |
| 44 | + try { |
| 45 | + DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams(textDocumentIdentifier, formattingOptions, LSPEclipseUtils.toPosition(command.offset, document), command.text); |
| 46 | + executor.computeFirst(ls -> ls.getTextDocumentService().onTypeFormatting(params)).join() |
| 47 | + .ifPresent(edits -> { |
| 48 | + edits.forEach(edit -> { |
| 49 | + try { |
| 50 | + int fromOffset = LSPEclipseUtils.toOffset(edit.getRange().getStart(), document); |
| 51 | + int toOffset = LSPEclipseUtils.toOffset(edit.getRange().getEnd(), document); |
| 52 | + if (fromOffset == command.offset) { |
| 53 | + command.text += edit.getNewText(); |
| 54 | + } else { |
| 55 | + command.addCommand(fromOffset, toOffset - fromOffset, edit.getNewText(), null); |
| 56 | + } |
| 57 | + } catch (BadLocationException ex) { |
| 58 | + LanguageServerPlugin.logError(ex); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + } catch (BadLocationException e) { |
| 63 | + LanguageServerPlugin.logError(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments