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
@@ -16,6 +16,7 @@ import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocum
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.semantics.PbsEditorialRename;
import p.studio.compiler.pbs.semantics.PbsEditorialWorkspaceSymbol;
import p.studio.compiler.pbs.PbsDocumentFormatter;
import p.studio.compiler.pbs.PbsStructureRanges;
import p.studio.compiler.pbs.semantics.PbsQuickFix;
import p.studio.compiler.pbs.semantics.PbsQuickFixCollector;
@@ -475,6 +476,17 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
offsets);
}
@Override
public boolean formattingSupported() {
return true;
}
@Override
public Optional<String> formatDocument(final FrontendDocumentRequest request) {
return Optional.of(PbsDocumentFormatter.format(
request == null || request.documentText() == null ? "" : request.documentText()));
}
@Override
public boolean codeActionsSupported() {
return true;
@@ -0,0 +1,179 @@
package p.studio.compiler.pbs;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.pbs.lexer.PbsTokenKind;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public final class PbsDocumentFormatter {
private static final int INDENT_SPACES = 4;
private PbsDocumentFormatter() {
}
public static String format(final String documentText) {
final String text = documentText == null ? "" : documentText;
if (text.isEmpty()) {
return text;
}
final ReadOnlyList<PbsToken> tokens = PbsLexer.lex(text, FileId.none(), DiagnosticSink.empty());
final ArrayList<SourceLine> lines = splitLines(text);
final StringBuilder formatted = new StringBuilder(text.length());
int depth = 0;
for (final SourceLine line : lines) {
if (isDocInterior(line, tokens)) {
formatted.append(line.content()).append(line.ending());
continue;
}
final int leadingClosers = leadingClosers(line, tokens);
final int indent = Math.max(0, depth - leadingClosers);
formatted.append(reindent(line.content(), indent)).append(line.ending());
depth = depthAfter(line, tokens, depth);
}
return formatted.toString();
}
private static boolean isDocInterior(final SourceLine line, final ReadOnlyList<PbsToken> tokens) {
for (final PbsToken token : tokens) {
if (token.kind() != PbsTokenKind.DOC_TEXT_BLOCK) {
continue;
}
if (line.startByte() > token.start() && line.endByte() <= token.end()) {
return true;
}
}
return false;
}
private static int leadingClosers(final SourceLine line, final ReadOnlyList<PbsToken> tokens) {
int closers = 0;
boolean seenCode = false;
for (final PbsToken token : tokens) {
if (token.start() < line.startByte() || token.start() >= line.endByte()) {
continue;
}
if (token.kind() == PbsTokenKind.COMMENT) {
continue;
}
if (!seenCode && isCloser(token.kind())) {
closers += 1;
continue;
}
seenCode = true;
}
return closers;
}
private static int depthAfter(
final SourceLine line,
final ReadOnlyList<PbsToken> tokens,
final int depth) {
int next = depth;
for (final PbsToken token : tokens) {
if (token.start() < line.startByte() || token.start() >= line.endByte()) {
continue;
}
if (isOpener(token.kind())) {
next += 1;
} else if (isCloser(token.kind())) {
next = Math.max(0, next - 1);
}
}
return next;
}
private static boolean isOpener(final PbsTokenKind kind) {
return kind == PbsTokenKind.LEFT_BRACE || kind == PbsTokenKind.LEFT_PAREN;
}
private static boolean isCloser(final PbsTokenKind kind) {
return kind == PbsTokenKind.RIGHT_BRACE || kind == PbsTokenKind.RIGHT_PAREN;
}
private static String reindent(final String content, final int indent) {
int index = 0;
while (index < content.length()) {
final char character = content.charAt(index);
if (character != ' ' && character != '\t') {
break;
}
index += 1;
}
if (index == content.length()) {
return content;
}
return " ".repeat(indent * INDENT_SPACES) + content.substring(index);
}
private static ArrayList<SourceLine> splitLines(final String text) {
final ArrayList<SourceLine> lines = new ArrayList<>();
final byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
int charIndex = 0;
int byteIndex = 0;
int lineCharStart = 0;
int lineByteStart = 0;
while (charIndex < text.length()) {
final int codePoint = text.codePointAt(charIndex);
final int charWidth = Character.charCount(codePoint);
final int byteWidth = utf8Length(codePoint);
if (codePoint == '\n') {
lines.add(new SourceLine(
text.substring(lineCharStart, charIndex),
"\n",
lineByteStart,
byteIndex));
charIndex += charWidth;
byteIndex += byteWidth;
lineCharStart = charIndex;
lineByteStart = byteIndex;
continue;
}
if (codePoint == '\r' && charIndex + charWidth < text.length() && text.codePointAt(charIndex + charWidth) == '\n') {
lines.add(new SourceLine(
text.substring(lineCharStart, charIndex),
"\r\n",
lineByteStart,
byteIndex));
charIndex += charWidth + 1;
byteIndex += byteWidth + 1;
lineCharStart = charIndex;
lineByteStart = byteIndex;
continue;
}
charIndex += charWidth;
byteIndex += byteWidth;
}
if (lineCharStart < text.length()) {
lines.add(new SourceLine(
text.substring(lineCharStart),
"",
lineByteStart,
bytes.length));
}
if (lines.isEmpty()) {
lines.add(new SourceLine(text, "", 0, bytes.length));
}
return lines;
}
private static int utf8Length(final int codePoint) {
if (codePoint <= 0x7F) {
return 1;
}
if (codePoint <= 0x7FF) {
return 2;
}
if (codePoint <= 0xFFFF) {
return 3;
}
return 4;
}
private record SourceLine(String content, String ending, int startByte, int endByte) {
}
}