refactor and clean up
This commit is contained in:
parent
0ac7d9e732
commit
2994bc67a3
@ -0,0 +1,6 @@
|
||||
package p.studio.lsp.messages;
|
||||
|
||||
public record DocumentPosition(
|
||||
int line,
|
||||
int character) {
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package p.studio.lsp.services.compiler;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import p.studio.compiler.messages.BuilderPipelineConfig;
|
||||
import p.studio.compiler.messages.BuildingIssue;
|
||||
import p.studio.compiler.models.AnalysisSnapshot;
|
||||
@ -12,7 +13,6 @@ import p.studio.lsp.services.LanguageServiceBridge;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -37,7 +37,7 @@ public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
|
||||
final String text) {
|
||||
Objects.requireNonNull(context, "context");
|
||||
final Path documentPath = normalizeDocumentPath(documentUri);
|
||||
final String effectiveText = text == null ? "" : text;
|
||||
final String effectiveText = StringUtils.isBlank(text) ? "" : text;
|
||||
final AnalysisSnapshot snapshot = analyzeProject(context, Map.of(documentPath, effectiveText));
|
||||
return new BaselineDocumentAnalysis(mapDiagnostics(snapshot, documentPath, effectiveText));
|
||||
}
|
||||
@ -121,60 +121,4 @@ public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
|
||||
private Path normalizeDocumentPath(final String documentUri) {
|
||||
return Path.of(URI.create(Objects.requireNonNull(documentUri, "documentUri"))).toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private record DocumentPosition(
|
||||
int line,
|
||||
int character) {
|
||||
}
|
||||
|
||||
private static final class DocumentPositionMapper {
|
||||
private final String text;
|
||||
|
||||
private DocumentPositionMapper(final String text) {
|
||||
this.text = Objects.requireNonNull(text, "text");
|
||||
}
|
||||
|
||||
private DocumentPosition positionOf(final Integer byteOffset) {
|
||||
if (byteOffset == null || byteOffset <= 0 || text.isEmpty()) {
|
||||
return new DocumentPosition(0, 0);
|
||||
}
|
||||
final int target = Math.min(byteOffset, text.getBytes(StandardCharsets.UTF_8).length);
|
||||
int utf8Offset = 0;
|
||||
int line = 0;
|
||||
int character = 0;
|
||||
for (int index = 0; index < text.length(); ) {
|
||||
if (utf8Offset >= target) {
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
final int codePoint = text.codePointAt(index);
|
||||
final int utf16Width = Character.charCount(codePoint);
|
||||
final int utf8Width = utf8Length(codePoint);
|
||||
if (utf8Offset + utf8Width > target) {
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
utf8Offset += utf8Width;
|
||||
if (codePoint == '\n') {
|
||||
line++;
|
||||
character = 0;
|
||||
} else {
|
||||
character += utf16Width;
|
||||
}
|
||||
index += utf16Width;
|
||||
}
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
|
||||
private int utf8Length(final int codePoint) {
|
||||
if (codePoint <= 0x7F) {
|
||||
return 1;
|
||||
}
|
||||
if (codePoint <= 0x7FF) {
|
||||
return 2;
|
||||
}
|
||||
if (codePoint <= 0xFFFF) {
|
||||
return 3;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
package p.studio.lsp.services.compiler;
|
||||
|
||||
import p.studio.lsp.messages.DocumentPosition;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
final class DocumentPositionMapper {
|
||||
private final String text;
|
||||
|
||||
DocumentPositionMapper(final String text) {
|
||||
this.text = Objects.requireNonNull(text, "text");
|
||||
}
|
||||
|
||||
DocumentPosition positionOf(final Integer byteOffset) {
|
||||
if (byteOffset == null || byteOffset <= 0 || text.isEmpty()) {
|
||||
return new DocumentPosition(0, 0);
|
||||
}
|
||||
final int target = Math.min(byteOffset, text.getBytes(StandardCharsets.UTF_8).length);
|
||||
int utf8Offset = 0;
|
||||
int line = 0;
|
||||
int character = 0;
|
||||
for (int index = 0; index < text.length(); ) {
|
||||
if (utf8Offset >= target) {
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
final int codePoint = text.codePointAt(index);
|
||||
final int utf16Width = Character.charCount(codePoint);
|
||||
final int utf8Width = utf8Length(codePoint);
|
||||
if (utf8Offset + utf8Width > target) {
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
utf8Offset += utf8Width;
|
||||
if (codePoint == '\n') {
|
||||
line++;
|
||||
character = 0;
|
||||
} else {
|
||||
character += utf16Width;
|
||||
}
|
||||
index += utf16Width;
|
||||
}
|
||||
return new DocumentPosition(line, character);
|
||||
}
|
||||
|
||||
private int utf8Length(final int codePoint) {
|
||||
if (codePoint <= 0x7F) {
|
||||
return 1;
|
||||
}
|
||||
if (codePoint <= 0x7FF) {
|
||||
return 2;
|
||||
}
|
||||
if (codePoint <= 0xFFFF) {
|
||||
return 3;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user