implements PLN-0137 (4/7) document-formatting

Advertise full-document formatting only. The formatter reprints the
token stream and changes indentation by four spaces where brace or
parenthesis nesting changes. Doc text-block interiors stay as written,
comments stay on their current line, and declarations are not reordered.
Range formatting and on-type formatting stay off.
This commit is contained in:
2026-09-22 08:50:18 +01:00
parent ce2e3e4cd3
commit 7629a73e0a
17 changed files with 366 additions and 0 deletions
@@ -0,0 +1,13 @@
package p.studio.lsp.messages;
import java.util.List;
public record BaselineFormatting(List<BaselineTextEdit> edits) {
public BaselineFormatting {
edits = edits == null ? List.of() : List.copyOf(edits);
}
public static BaselineFormatting empty() {
return new BaselineFormatting(List.of());
}
}
@@ -19,6 +19,7 @@ public record BaselineServerDescription(
boolean documentLinksSupported,
boolean foldingRangesSupported,
boolean selectionRangesSupported,
boolean formattingSupported,
List<String> semanticTokenTypes,
List<BaselineSemanticHostProjection> semanticHostProjections,
List<BaselineVisualTheme> visualThemes,
@@ -0,0 +1,18 @@
package p.studio.lsp.messages;
import java.util.Objects;
public record BaselineTextEdit(
int startLine,
int startCharacter,
int endLine,
int endCharacter,
String newText) {
public BaselineTextEdit {
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("text edit coordinates must not be negative");
}
newText = Objects.requireNonNull(newText, "newText");
}
}
@@ -11,6 +11,7 @@ import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselineDocumentLinks;
import p.studio.lsp.messages.BaselineFoldingRanges;
import p.studio.lsp.messages.BaselineFormatting;
import p.studio.lsp.messages.BaselineSelectionRanges;
import p.studio.lsp.messages.DocumentPosition;
import p.studio.lsp.messages.BaselinePrepareRename;
@@ -72,6 +73,10 @@ public interface LanguageServiceBridge {
return BaselineFoldingRanges.empty();
}
default BaselineFormatting formatDocument(LspProjectContext context, String documentUri, String text) {
return BaselineFormatting.empty();
}
default BaselineSelectionRanges selectionRanges(
LspProjectContext context,
String documentUri,
@@ -69,6 +69,9 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
final boolean selectionRangesSupported = languageService
.map(FrontendLanguageService::selectionRangesSupported)
.orElse(false);
final boolean formattingSupported = languageService
.map(FrontendLanguageService::formattingSupported)
.orElse(false);
return new BaselineServerDescription(
"Prometeu Studio LSP",
"0.1.0",
@@ -85,6 +88,7 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
documentLinksSupported,
foldingRangesSupported,
selectionRangesSupported,
formattingSupported,
presentation.semanticKeys(),
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
presentation.themes().stream().map(this::mapVisualTheme).toList(),
@@ -403,6 +407,34 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
.orElseGet(BaselineDocumentLinks::empty);
}
@Override
public BaselineFormatting formatDocument(
final LspProjectContext context,
final String documentUri,
final String text) {
Objects.requireNonNull(context, "context");
final var languageService = frontendProvider(context).languageService();
if (languageService.isEmpty() || !languageService.orElseThrow().formattingSupported()) {
return BaselineFormatting.empty();
}
final String effectiveText = text == null ? "" : text;
final String formatted = languageService.orElseThrow().formatDocument(new FrontendDocumentRequest(
context.projectRoot(),
normalizeDocumentPath(documentUri),
effectiveText)).orElse(effectiveText);
if (formatted.equals(effectiveText)) {
return BaselineFormatting.empty();
}
final DocumentPosition end = new DocumentPositionMapper(effectiveText)
.positionOf(effectiveText.getBytes(StandardCharsets.UTF_8).length);
return new BaselineFormatting(List.of(new BaselineTextEdit(
0,
0,
end.line(),
end.character(),
formatted)));
}
@Override
public BaselineFoldingRanges foldingRanges(
final LspProjectContext context,
@@ -169,6 +169,14 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
return CompletableFuture.completedFuture(protocolMessageMapper.mapRename(result));
}
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(final DocumentFormattingParams params) {
final String uri = params.getTextDocument().getUri();
final String text = documentTextByUri.get(uri);
return CompletableFuture.completedFuture(protocolMessageMapper.mapFormatting(
languageServiceBridge.formatDocument(project, uri, text)));
}
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(final FoldingRangeRequestParams params) {
final String uri = params.getTextDocument().getUri();
@@ -64,6 +64,9 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
if (description.selectionRangesSupported()) {
capabilities.setSelectionRangeProvider(true);
}
if (description.formattingSupported()) {
capabilities.setDocumentFormattingProvider(true);
}
final SemanticTokensWithRegistrationOptions semanticTokens = new SemanticTokensWithRegistrationOptions();
semanticTokens.setLegend(new SemanticTokensLegend(description.semanticTokenTypes(), List.of()));
semanticTokens.setFull(true);
@@ -187,6 +190,22 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
return List.copyOf(mapped);
}
@Override
public List<TextEdit> mapFormatting(final BaselineFormatting formatting) {
if (formatting == null || formatting.edits().isEmpty()) {
return List.of();
}
final ArrayList<TextEdit> mapped = new ArrayList<>();
for (final BaselineTextEdit edit : formatting.edits()) {
mapped.add(new TextEdit(
new Range(
new Position(edit.startLine(), edit.startCharacter()),
new Position(edit.endLine(), edit.endCharacter())),
edit.newText()));
}
return List.copyOf(mapped);
}
@Override
public List<FoldingRange> mapFoldingRanges(final BaselineFoldingRanges ranges) {
if (ranges == null || ranges.ranges().isEmpty()) {
@@ -12,6 +12,7 @@ import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.FoldingRange;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.SelectionRange;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
@@ -24,6 +25,7 @@ import org.eclipse.lsp4j.PrepareRenameResult;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineDocumentLinks;
import p.studio.lsp.messages.BaselineFoldingRanges;
import p.studio.lsp.messages.BaselineFormatting;
import p.studio.lsp.messages.BaselineSelectionRanges;
import p.studio.lsp.messages.BaselineDocumentSymbols;
import p.studio.lsp.messages.BaselineWorkspaceSymbols;
@@ -73,6 +75,10 @@ public interface ProtocolMessageMapper {
return List.of();
}
default List<TextEdit> mapFormatting(BaselineFormatting formatting) {
return List.of();
}
default List<SelectionRange> mapSelectionRanges(BaselineSelectionRanges ranges) {
return List.of();
}