implements PLN-0135
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 63.56% (19159/30145)
* Branch Coverage: 54.14% (7612/14060)
* Lines of Code: 30145
* Cyclomatic Complexity: 12214
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 678
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 63.56% (19159/30145)
* Branch Coverage: 54.14% (7612/14060)
* Lines of Code: 30145
* Cyclomatic Complexity: 12214
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 678
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Add PBS rename as a validated WorkspaceEdit of physical identifier sites on FrontendLanguageService. Identity is definition; locals and params rename from their name span. Refuse aliases, virtual stdlib, builtins, error labels, keywords, and collisions. Advertise renameProvider with prepareProvider; refused cursors are LSP errors, not empty success. Housekeep DSC-0043 with LSN-0072.
This commit is contained in:
+98
@@ -13,12 +13,15 @@ import p.studio.compiler.pbs.semantics.PbsEditorialSignatureHelp;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSymbolKind;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocument;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialRename;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialWorkspaceSymbol;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
|
||||
import p.studio.compiler.services.FrontendCompletionCandidate;
|
||||
import p.studio.compiler.services.FrontendDefinitionLocation;
|
||||
import p.studio.compiler.services.FrontendDocumentRequest;
|
||||
import p.studio.compiler.services.FrontendDocumentSymbol;
|
||||
import p.studio.compiler.services.FrontendRenameEdit;
|
||||
import p.studio.compiler.services.FrontendRenameResult;
|
||||
import p.studio.compiler.services.FrontendDocumentation;
|
||||
import p.studio.compiler.services.FrontendEditorialContext;
|
||||
import p.studio.compiler.services.FrontendHover;
|
||||
@@ -337,6 +340,101 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return List.copyOf(symbols);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<FrontendDefinitionLocation> prepareRename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset) {
|
||||
final PbsEditorialRename planned = editorialRename(request, editorialContext, offset, null);
|
||||
if (planned == null || !planned.allowed()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (!(editorialContext.sourceFiles() instanceof FileTableReader fileTable)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (physicalIdentity(planned, fileTable).isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return toFrontendLocation(planned.placeholder(), fileTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FrontendRenameResult rename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
if (newName == null) {
|
||||
return FrontendRenameResult.refused("invalid identifier");
|
||||
}
|
||||
final PbsEditorialRename planned = editorialRename(request, editorialContext, offset, newName);
|
||||
if (planned == null) {
|
||||
return FrontendRenameResult.refused("not renameable");
|
||||
}
|
||||
if (!planned.allowed()) {
|
||||
return FrontendRenameResult.refused(planned.refusal());
|
||||
}
|
||||
if (!(editorialContext.sourceFiles() instanceof FileTableReader fileTable)) {
|
||||
return FrontendRenameResult.refused("not renameable");
|
||||
}
|
||||
if (physicalIdentity(planned, fileTable).isEmpty()) {
|
||||
return FrontendRenameResult.refused("not renameable");
|
||||
}
|
||||
final LinkedHashMap<String, FrontendRenameEdit> edits = new LinkedHashMap<>();
|
||||
for (final PbsEditorialLocation site : planned.sites()) {
|
||||
toFrontendLocation(site, fileTable).ifPresent(location ->
|
||||
edits.putIfAbsent(
|
||||
frontendLocationKey(location),
|
||||
new FrontendRenameEdit(
|
||||
location.path(),
|
||||
location.startOffset(),
|
||||
location.endOffset(),
|
||||
newName)));
|
||||
}
|
||||
if (edits.isEmpty()) {
|
||||
return FrontendRenameResult.refused("not renameable");
|
||||
}
|
||||
return FrontendRenameResult.applied(List.copyOf(edits.values()));
|
||||
}
|
||||
|
||||
private PbsEditorialRename editorialRename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
if (request == null
|
||||
|| editorialContext == null
|
||||
|| !(editorialContext.syntaxTree() instanceof PbsAst.File ast)
|
||||
|| !(editorialContext.supplementalDeclarations() instanceof ReadOnlyList<?> supplementalTopDecls)
|
||||
|| !(editorialContext.semanticContext() instanceof FESurfaceContext feSurfaceContext)
|
||||
|| !(editorialContext.sourceFiles() instanceof FileTableReader fileTable)) {
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
final ReadOnlyList<PbsAst.TopDecl> typedSupplementalTopDecls =
|
||||
(ReadOnlyList<PbsAst.TopDecl>) supplementalTopDecls;
|
||||
final EditorialDocument cursorDocument = new EditorialDocument(
|
||||
request.documentText(),
|
||||
ast,
|
||||
typedSupplementalTopDecls,
|
||||
feSurfaceContext);
|
||||
return editorialSupportService.rename(
|
||||
cursorDocument,
|
||||
offset,
|
||||
newName,
|
||||
projectDocuments(request, editorialContext, cursorDocument, fileTable));
|
||||
}
|
||||
|
||||
private List<FrontendDefinitionLocation> physicalIdentity(
|
||||
final PbsEditorialRename planned,
|
||||
final FileTableReader fileTable) {
|
||||
final ArrayList<FrontendDefinitionLocation> locations = new ArrayList<>();
|
||||
for (final PbsEditorialLocation location : planned.identity()) {
|
||||
toFrontendLocation(location, fileTable).ifPresent(locations::add);
|
||||
}
|
||||
return List.copyOf(locations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendSemanticToken> semanticTokens(final FrontendDocumentRequest request) {
|
||||
return semanticTokens(request == null ? "" : request.documentText());
|
||||
|
||||
+26
@@ -46,6 +46,32 @@ public final class PbsLexer {
|
||||
return lexer.lexInternal();
|
||||
}
|
||||
|
||||
public static boolean isKeyword(final String text) {
|
||||
return text != null && KEYWORDS.containsKey(text);
|
||||
}
|
||||
|
||||
public static boolean isIdentifierLexeme(final String text) {
|
||||
if (text == null || text.isEmpty() || isKeyword(text)) {
|
||||
return false;
|
||||
}
|
||||
int index = 0;
|
||||
final int first = text.codePointAt(0);
|
||||
if (!(first == '_' || Character.isAlphabetic(first))) {
|
||||
return false;
|
||||
}
|
||||
index += Character.charCount(first);
|
||||
while (index < text.length()) {
|
||||
final int codePoint = text.codePointAt(index);
|
||||
if (!(codePoint == '_'
|
||||
|| Character.isAlphabetic(codePoint)
|
||||
|| Character.isDigit(codePoint))) {
|
||||
return false;
|
||||
}
|
||||
index += Character.charCount(codePoint);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private ReadOnlyList<PbsToken> lexInternal() {
|
||||
while (!isAtEnd() || state != LexerState.DEFAULT) {
|
||||
switch (state) {
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PbsEditorialRename(
|
||||
String refusal,
|
||||
PbsEditorialLocation placeholder,
|
||||
List<PbsEditorialLocation> identity,
|
||||
List<PbsEditorialLocation> sites) {
|
||||
|
||||
public PbsEditorialRename {
|
||||
refusal = refusal == null ? "" : refusal;
|
||||
identity = identity == null ? List.of() : List.copyOf(identity);
|
||||
sites = sites == null ? List.of() : List.copyOf(sites);
|
||||
}
|
||||
|
||||
public static PbsEditorialRename refused(final String reason) {
|
||||
return new PbsEditorialRename(
|
||||
reason == null || reason.isBlank() ? "not renameable" : reason,
|
||||
null,
|
||||
List.of(),
|
||||
List.of());
|
||||
}
|
||||
|
||||
public static PbsEditorialRename applied(
|
||||
final PbsEditorialLocation placeholder,
|
||||
final List<PbsEditorialLocation> identity,
|
||||
final List<PbsEditorialLocation> sites) {
|
||||
return new PbsEditorialRename(
|
||||
"",
|
||||
Objects.requireNonNull(placeholder, "placeholder"),
|
||||
identity,
|
||||
sites);
|
||||
}
|
||||
|
||||
public boolean allowed() {
|
||||
return refusal.isBlank() && placeholder != null && !identity.isEmpty() && !sites.isEmpty();
|
||||
}
|
||||
}
|
||||
+464
@@ -155,6 +155,63 @@ public final class PbsEditorialSupportService {
|
||||
return List.copyOf(usages.values());
|
||||
}
|
||||
|
||||
public Optional<PbsEditorialLocation> prepareRename(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final int offset) {
|
||||
return prepareRename(sourceText, ast, supplementalTopDecls, FESurfaceContext.empty(), offset);
|
||||
}
|
||||
|
||||
public Optional<PbsEditorialLocation> prepareRename(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final int offset) {
|
||||
final EditorialDocument cursor = new EditorialDocument(sourceText, ast, supplementalTopDecls, feSurfaceContext);
|
||||
return prepareRename(cursor, offset, List.of(cursor));
|
||||
}
|
||||
|
||||
public Optional<PbsEditorialLocation> prepareRename(
|
||||
final EditorialDocument cursorDocument,
|
||||
final int offset,
|
||||
final List<EditorialDocument> projectDocuments) {
|
||||
final PbsEditorialRename planned = planRename(cursorDocument, offset, projectDocuments, null);
|
||||
if (!planned.allowed()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(planned.placeholder());
|
||||
}
|
||||
|
||||
public PbsEditorialRename rename(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
return rename(sourceText, ast, supplementalTopDecls, FESurfaceContext.empty(), offset, newName);
|
||||
}
|
||||
|
||||
public PbsEditorialRename rename(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
final EditorialDocument cursor = new EditorialDocument(sourceText, ast, supplementalTopDecls, feSurfaceContext);
|
||||
return rename(cursor, offset, newName, List.of(cursor));
|
||||
}
|
||||
|
||||
public PbsEditorialRename rename(
|
||||
final EditorialDocument cursorDocument,
|
||||
final int offset,
|
||||
final String newName,
|
||||
final List<EditorialDocument> projectDocuments) {
|
||||
return planRename(cursorDocument, offset, projectDocuments, newName);
|
||||
}
|
||||
|
||||
public record EditorialDocument(
|
||||
String sourceText,
|
||||
PbsAst.File ast,
|
||||
@@ -202,6 +259,76 @@ public final class PbsEditorialSupportService {
|
||||
return Optional.of(new PbsEditorialSignatureHelp(signatures, 0, callSite.activeParameter()));
|
||||
}
|
||||
|
||||
private PbsEditorialRename planRename(
|
||||
final EditorialDocument cursorDocument,
|
||||
final int offset,
|
||||
final List<EditorialDocument> projectDocuments,
|
||||
final String newName) {
|
||||
if (cursorDocument == null || cursorDocument.ast() == null) {
|
||||
return PbsEditorialRename.refused("not renameable");
|
||||
}
|
||||
final DocumentContext cursor = buildContext(
|
||||
cursorDocument.sourceText(),
|
||||
cursorDocument.ast(),
|
||||
cursorDocument.supplementalTopDecls(),
|
||||
cursorDocument.feSurfaceContext());
|
||||
final RenameCursor renameCursor = cursor.renameCursorAt(offset);
|
||||
if (renameCursor.refusal() != null) {
|
||||
return PbsEditorialRename.refused(renameCursor.refusal());
|
||||
}
|
||||
if (newName != null) {
|
||||
if (!PbsLexer.isIdentifierLexeme(newName)) {
|
||||
return PbsEditorialRename.refused("invalid identifier");
|
||||
}
|
||||
if (cursor.collides(newName, renameCursor)) {
|
||||
return PbsEditorialRename.refused("name collision");
|
||||
}
|
||||
}
|
||||
final List<EditorialDocument> documents = projectDocuments == null || projectDocuments.isEmpty()
|
||||
? List.of(cursorDocument)
|
||||
: projectDocuments;
|
||||
final LinkedHashMap<String, PbsEditorialLocation> sites = new LinkedHashMap<>();
|
||||
sites.putIfAbsent(locationKey(renameCursor.placeholder().span()), renameCursor.placeholder());
|
||||
final Set<String> identityKeys = locationKeys(renameCursor.identity());
|
||||
for (final EditorialDocument document : documents) {
|
||||
if (document == null || document.ast() == null) {
|
||||
continue;
|
||||
}
|
||||
final DocumentContext documentContext = buildContext(
|
||||
document.sourceText(),
|
||||
document.ast(),
|
||||
document.supplementalTopDecls(),
|
||||
document.feSurfaceContext());
|
||||
for (final PbsEditorialLocation site : documentContext.renameSitesMatching(
|
||||
identityKeys,
|
||||
renameCursor.originalName())) {
|
||||
sites.putIfAbsent(locationKey(site.span()), site);
|
||||
}
|
||||
final FileId documentFileId = document.ast().span() == null
|
||||
? FileId.none()
|
||||
: document.ast().span().getFileId();
|
||||
for (final PbsEditorialLocation location : renameCursor.identity()) {
|
||||
if (location == null || location.span() == null || location.span().isNone()) {
|
||||
continue;
|
||||
}
|
||||
if (location.span().length() != renameCursor.originalName().length()) {
|
||||
continue;
|
||||
}
|
||||
if (location.span().getFileId() == null || !location.span().getFileId().equals(documentFileId)) {
|
||||
continue;
|
||||
}
|
||||
sites.putIfAbsent(locationKey(location.span()), location);
|
||||
}
|
||||
}
|
||||
if (sites.isEmpty()) {
|
||||
return PbsEditorialRename.refused("not renameable");
|
||||
}
|
||||
return PbsEditorialRename.applied(
|
||||
renameCursor.placeholder(),
|
||||
renameCursor.identity(),
|
||||
List.copyOf(sites.values()));
|
||||
}
|
||||
|
||||
private DocumentContext buildContext(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
@@ -1499,6 +1626,332 @@ public final class PbsEditorialSupportService {
|
||||
throw new IndexOutOfBoundsException("No significant token at index " + significantIndex);
|
||||
}
|
||||
|
||||
private RenameCursor renameCursorAt(final int offset) {
|
||||
final Integer tokenIndex = identifierTokenIndexAt(offset);
|
||||
if (tokenIndex == null) {
|
||||
return RenameCursor.refused("not an identifier");
|
||||
}
|
||||
final PbsToken token = token(tokenIndex);
|
||||
final String lexeme = token.lexeme();
|
||||
if (isImportAliasLexeme(lexeme)) {
|
||||
return RenameCursor.refused("import alias");
|
||||
}
|
||||
List<PbsEditorialLocation> identity = definitionAt(offset);
|
||||
if (identity.isEmpty()) {
|
||||
identity = localLocations(lexeme, offset);
|
||||
}
|
||||
if (identity.isEmpty()) {
|
||||
identity = parameterLocations(lexeme, offset);
|
||||
}
|
||||
if (isErrorCaseLabel(lexeme, offset, identity)) {
|
||||
return RenameCursor.refused("error case label");
|
||||
}
|
||||
if (identity.isEmpty()) {
|
||||
return RenameCursor.refused("not renameable");
|
||||
}
|
||||
final FileId fileId = ast.span() == null ? FileId.none() : ast.span().getFileId();
|
||||
final Span placeholderSpan = new Span(fileId, token.start(), token.end());
|
||||
if (!hasPhysicalSpan(placeholderSpan)) {
|
||||
return RenameCursor.refused("not renameable");
|
||||
}
|
||||
return new RenameCursor(
|
||||
lexeme,
|
||||
new PbsEditorialLocation(placeholderSpan),
|
||||
identity,
|
||||
null);
|
||||
}
|
||||
|
||||
private List<PbsEditorialLocation> renameSitesMatching(
|
||||
final Set<String> identityKeys,
|
||||
final String originalName) {
|
||||
if (identityKeys == null || identityKeys.isEmpty() || originalName == null || originalName.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
final FileId fileId = ast.span() == null ? FileId.none() : ast.span().getFileId();
|
||||
final LinkedHashMap<String, PbsEditorialLocation> sites = new LinkedHashMap<>();
|
||||
for (final PbsToken token : tokens) {
|
||||
if (token.kind() != PbsTokenKind.IDENTIFIER || !originalName.equals(token.lexeme())) {
|
||||
continue;
|
||||
}
|
||||
if (isImportAliasLexeme(token.lexeme())) {
|
||||
continue;
|
||||
}
|
||||
if (!intersects(definitionAt(token.start()), identityKeys)) {
|
||||
continue;
|
||||
}
|
||||
final Span usageSpan = new Span(fileId, token.start(), token.end());
|
||||
if (!hasPhysicalSpan(usageSpan)) {
|
||||
continue;
|
||||
}
|
||||
sites.putIfAbsent(locationKey(usageSpan), new PbsEditorialLocation(usageSpan));
|
||||
}
|
||||
return List.copyOf(sites.values());
|
||||
}
|
||||
|
||||
private boolean isImportAliasLexeme(final String lexeme) {
|
||||
if (lexeme == null || lexeme.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
for (final var importDecl : ast.imports()) {
|
||||
for (final var item : importDecl.items()) {
|
||||
if (item.alias() == null || item.alias().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (lexeme.equals(item.alias()) && !item.alias().equals(item.name())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isErrorCaseLabel(
|
||||
final String lexeme,
|
||||
final int offset,
|
||||
final List<PbsEditorialLocation> identity) {
|
||||
if (lexeme == null || lexeme.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.ErrorDecl errorDecl)) {
|
||||
continue;
|
||||
}
|
||||
if (lexeme.equals(errorDecl.name()) || !errorDecl.cases().asList().contains(lexeme)) {
|
||||
continue;
|
||||
}
|
||||
if (errorDecl.span() != null && errorDecl.span().contains(offset)) {
|
||||
return true;
|
||||
}
|
||||
final String errorKey = locationKey(errorDecl.span());
|
||||
for (final PbsEditorialLocation location : identity) {
|
||||
if (location != null && location.span() != null && errorKey.equals(locationKey(location.span()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean collides(
|
||||
final String newName,
|
||||
final RenameCursor renameCursor) {
|
||||
if (newName == null || newName.equals(renameCursor.originalName())) {
|
||||
return false;
|
||||
}
|
||||
if (isLocalOrParameterIdentity(renameCursor)) {
|
||||
return localBindingNames((int) renameCursor.placeholder().span().getStart()).contains(newName);
|
||||
}
|
||||
if (isStructFieldIdentity(renameCursor)) {
|
||||
return structFieldNames(renameCursor).contains(newName);
|
||||
}
|
||||
if (isMethodIdentity(renameCursor)) {
|
||||
return methodNames(renameCursor).contains(newName);
|
||||
}
|
||||
if (isEnumCaseIdentity(renameCursor)) {
|
||||
return enumCaseNames(renameCursor).contains(newName);
|
||||
}
|
||||
return topLevelBindingNames().contains(newName);
|
||||
}
|
||||
|
||||
private boolean isLocalOrParameterIdentity(final RenameCursor renameCursor) {
|
||||
final Set<String> keys = locationKeys(renameCursor.identity());
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.FunctionDecl functionDecl) || functionDecl.body() == null) {
|
||||
continue;
|
||||
}
|
||||
for (final var parameter : functionDecl.parameters()) {
|
||||
if (keys.contains(locationKey(parameter.span()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (containsLetNameSpan(functionDecl.body(), keys)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.StructDecl structDecl)) {
|
||||
continue;
|
||||
}
|
||||
for (final var method : structDecl.methods()) {
|
||||
if (method.body() == null) {
|
||||
continue;
|
||||
}
|
||||
for (final var parameter : method.parameters()) {
|
||||
if (keys.contains(locationKey(parameter.span()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (containsLetNameSpan(method.body(), keys)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean containsLetNameSpan(
|
||||
final PbsAst.Block block,
|
||||
final Set<String> keys) {
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
for (final var statement : block.statements()) {
|
||||
if (statement instanceof PbsAst.LetStatement letStatement
|
||||
&& letStatement.nameSpan() != null
|
||||
&& keys.contains(locationKey(letStatement.nameSpan()))) {
|
||||
return true;
|
||||
}
|
||||
if (statement instanceof PbsAst.IfStatement ifStatement) {
|
||||
if (containsLetNameSpan(ifStatement.thenBlock(), keys)
|
||||
|| containsLetNameSpan(ifStatement.elseBlock(), keys)) {
|
||||
return true;
|
||||
}
|
||||
} else if (statement instanceof PbsAst.ForStatement forStatement) {
|
||||
if (containsLetNameSpan(forStatement.body(), keys)) {
|
||||
return true;
|
||||
}
|
||||
} else if (statement instanceof PbsAst.WhileStatement whileStatement
|
||||
&& containsLetNameSpan(whileStatement.body(), keys)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Set<String> localBindingNames(final int offset) {
|
||||
final LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
final CallableFrame frame = callableFrameAt(offset);
|
||||
if (frame == null) {
|
||||
return Set.of();
|
||||
}
|
||||
names.addAll(frame.parameterNames());
|
||||
collectLetNames(frame.body(), names);
|
||||
return names;
|
||||
}
|
||||
|
||||
private void collectLetNames(
|
||||
final PbsAst.Block block,
|
||||
final Set<String> names) {
|
||||
if (block == null) {
|
||||
return;
|
||||
}
|
||||
for (final var statement : block.statements()) {
|
||||
if (statement instanceof PbsAst.LetStatement letStatement) {
|
||||
names.add(letStatement.name());
|
||||
} else if (statement instanceof PbsAst.IfStatement ifStatement) {
|
||||
collectLetNames(ifStatement.thenBlock(), names);
|
||||
collectLetNames(ifStatement.elseBlock(), names);
|
||||
} else if (statement instanceof PbsAst.ForStatement forStatement) {
|
||||
names.add(forStatement.iteratorName());
|
||||
collectLetNames(forStatement.body(), names);
|
||||
} else if (statement instanceof PbsAst.WhileStatement whileStatement) {
|
||||
collectLetNames(whileStatement.body(), names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isStructFieldIdentity(final RenameCursor renameCursor) {
|
||||
return !structFieldNames(renameCursor).isEmpty()
|
||||
&& structFieldIdentityMatch(renameCursor);
|
||||
}
|
||||
|
||||
private boolean structFieldIdentityMatch(final RenameCursor renameCursor) {
|
||||
final Set<String> keys = locationKeys(renameCursor.identity());
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.StructDecl structDecl)) {
|
||||
continue;
|
||||
}
|
||||
for (final var field : structDecl.fields()) {
|
||||
if (keys.contains(locationKey(field.span()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Set<String> structFieldNames(final RenameCursor renameCursor) {
|
||||
final Set<String> keys = locationKeys(renameCursor.identity());
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.StructDecl structDecl)) {
|
||||
continue;
|
||||
}
|
||||
for (final var field : structDecl.fields()) {
|
||||
if (keys.contains(locationKey(field.span()))) {
|
||||
final LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
for (final var sibling : structDecl.fields()) {
|
||||
names.add(sibling.name());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
private boolean isMethodIdentity(final RenameCursor renameCursor) {
|
||||
return !methodNames(renameCursor).isEmpty();
|
||||
}
|
||||
|
||||
private Set<String> methodNames(final RenameCursor renameCursor) {
|
||||
final Set<String> keys = locationKeys(renameCursor.identity());
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.StructDecl structDecl)) {
|
||||
continue;
|
||||
}
|
||||
for (final var method : structDecl.methods()) {
|
||||
if (keys.contains(locationKey(method.span()))) {
|
||||
final LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
for (final var sibling : structDecl.methods()) {
|
||||
names.add(sibling.name());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
private boolean isEnumCaseIdentity(final RenameCursor renameCursor) {
|
||||
return !enumCaseNames(renameCursor).isEmpty();
|
||||
}
|
||||
|
||||
private Set<String> enumCaseNames(final RenameCursor renameCursor) {
|
||||
final Set<String> keys = locationKeys(renameCursor.identity());
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.EnumDecl enumDecl)) {
|
||||
continue;
|
||||
}
|
||||
for (final var enumCase : enumDecl.cases()) {
|
||||
if (keys.contains(locationKey(enumCase.span()))) {
|
||||
final LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
for (final var sibling : enumDecl.cases()) {
|
||||
names.add(sibling.name());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
private Set<String> topLevelBindingNames() {
|
||||
final LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
|
||||
names.add(functionDecl.name());
|
||||
} else if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
|
||||
names.add(serviceDecl.name());
|
||||
} else if (topDecl instanceof PbsAst.GlobalDecl globalDecl) {
|
||||
names.add(globalDecl.name());
|
||||
} else if (topDecl instanceof PbsAst.ConstDecl constDecl) {
|
||||
names.add(constDecl.name());
|
||||
}
|
||||
}
|
||||
names.addAll(importedSymbols.keySet());
|
||||
return names;
|
||||
}
|
||||
|
||||
private PbsToken token(final int index) {
|
||||
return tokens.get(index);
|
||||
}
|
||||
@@ -1707,6 +2160,17 @@ public final class PbsEditorialSupportService {
|
||||
String origin) {
|
||||
}
|
||||
|
||||
private record RenameCursor(
|
||||
String originalName,
|
||||
PbsEditorialLocation placeholder,
|
||||
List<PbsEditorialLocation> identity,
|
||||
String refusal) {
|
||||
|
||||
private static RenameCursor refused(final String reason) {
|
||||
return new RenameCursor("", null, List.of(), reason);
|
||||
}
|
||||
}
|
||||
|
||||
private record CallableFrame(
|
||||
ReadOnlyList<PbsAst.Parameter> parameters,
|
||||
PbsAst.Block body,
|
||||
|
||||
+8
@@ -8,6 +8,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsLexerTest {
|
||||
@@ -34,6 +35,13 @@ class PbsLexerTest {
|
||||
.kind());
|
||||
assertEquals(PbsTokenKind.EOF, tokens.getLast().kind());
|
||||
assertTrue(diagnostics.isEmpty(), "Lexer should not report diagnostics for valid input");
|
||||
assertTrue(PbsLexer.isKeyword("fn"));
|
||||
assertTrue(PbsLexer.isKeyword("step"));
|
||||
assertTrue(PbsLexer.isIdentifierLexeme("helper"));
|
||||
assertTrue(PbsLexer.isIdentifierLexeme("_value"));
|
||||
assertFalse(PbsLexer.isIdentifierLexeme("fn"));
|
||||
assertFalse(PbsLexer.isIdentifierLexeme("1ab"));
|
||||
assertFalse(PbsLexer.isIdentifierLexeme(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+126
@@ -606,6 +606,132 @@ final class PbsEditorialSupportServiceTest {
|
||||
assertEquals(List.of("r", "pack"), names(symbols.get(1).children()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameRewritesPhysicalFunctionSitesAndSkipsCommentsStringsAndCollisions() {
|
||||
final var source = """
|
||||
fn helper() -> int { return 1; }
|
||||
|
||||
fn helper(value: int) -> int { return value; }
|
||||
|
||||
fn frame() -> void {
|
||||
helper();
|
||||
helper(2);
|
||||
// helper
|
||||
let text = "helper";
|
||||
}
|
||||
""";
|
||||
final var ast = parseOrdinary(source);
|
||||
final var renamed = service.rename(source, ast, ReadOnlyList.empty(), source.indexOf("helper();"), "aid");
|
||||
assertTrue(renamed.allowed(), renamed.refusal());
|
||||
assertEquals(4, renamed.sites().size(), renamed.sites().toString());
|
||||
assertTrue(service.prepareRename(source, ast, ReadOnlyList.empty(), source.indexOf("helper();")).isPresent());
|
||||
|
||||
final var keyword = service.rename(source, ast, ReadOnlyList.empty(), source.indexOf("helper();"), "fn");
|
||||
assertFalse(keyword.allowed());
|
||||
assertEquals("invalid identifier", keyword.refusal());
|
||||
|
||||
final var collision = service.rename(source, ast, ReadOnlyList.empty(), source.indexOf("helper();"), "frame");
|
||||
assertFalse(collision.allowed());
|
||||
assertEquals("name collision", collision.refusal());
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameAllowsLocalNameSpanAndRefusesAliasBuiltinAndErrorLabel() {
|
||||
final var source = """
|
||||
import { Log as Logger } from @sdk:log;
|
||||
|
||||
declare error Boom {
|
||||
Missing;
|
||||
}
|
||||
|
||||
fn frame() -> void {
|
||||
let total = 1;
|
||||
Logger.info("x");
|
||||
let n: int = total;
|
||||
n;
|
||||
}
|
||||
""";
|
||||
final var ast = parseOrdinary(source);
|
||||
final var supplemental = supplementalTopDecls("""
|
||||
declare service Log {
|
||||
fn info(message: str) -> void {
|
||||
return;
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
final var local = service.rename(
|
||||
source,
|
||||
ast,
|
||||
supplemental,
|
||||
source.indexOf("total ="),
|
||||
"sum");
|
||||
assertTrue(local.allowed(), local.refusal());
|
||||
assertEquals(2, local.sites().size(), local.sites().toString());
|
||||
|
||||
final var alias = service.prepareRename(
|
||||
source,
|
||||
ast,
|
||||
supplemental,
|
||||
source.indexOf("Logger.info"));
|
||||
assertTrue(alias.isEmpty());
|
||||
|
||||
final var builtin = service.prepareRename(
|
||||
source,
|
||||
ast,
|
||||
supplemental,
|
||||
source.indexOf("int = total"));
|
||||
assertTrue(builtin.isEmpty());
|
||||
|
||||
final var errorLabel = service.prepareRename(
|
||||
source,
|
||||
ast,
|
||||
supplemental,
|
||||
source.indexOf("Missing"));
|
||||
assertTrue(errorLabel.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameOfExportDoesNotRewriteDifferingAlias() {
|
||||
final var exporter = """
|
||||
fn helper() -> int { return 1; }
|
||||
""";
|
||||
final var importer = """
|
||||
import { helper as aid } from @sdk:log;
|
||||
|
||||
fn frame() -> void {
|
||||
aid();
|
||||
}
|
||||
""";
|
||||
final var exporterAst = parse(exporter, new FileId(1), PbsParser.ParseMode.ORDINARY);
|
||||
final var importerAst = parseOrdinary(importer);
|
||||
final var exporterDoc = new PbsEditorialSupportService.EditorialDocument(
|
||||
exporter,
|
||||
exporterAst,
|
||||
ReadOnlyList.empty(),
|
||||
p.studio.compiler.messages.FESurfaceContext.empty());
|
||||
final var importerDoc = new PbsEditorialSupportService.EditorialDocument(
|
||||
importer,
|
||||
importerAst,
|
||||
exporterAst.topDecls(),
|
||||
p.studio.compiler.messages.FESurfaceContext.empty());
|
||||
final var renamed = service.rename(
|
||||
exporterDoc,
|
||||
exporter.indexOf("helper"),
|
||||
"renamed",
|
||||
List.of(exporterDoc, importerDoc));
|
||||
assertTrue(renamed.allowed(), renamed.refusal());
|
||||
assertEquals(2, renamed.sites().size(), renamed.sites().toString());
|
||||
assertTrue(renamed.sites().stream().anyMatch(site ->
|
||||
site.span().getFileId().equals(exporterAst.span().getFileId())));
|
||||
assertTrue(renamed.sites().stream().anyMatch(site ->
|
||||
site.span().getFileId().equals(importerAst.span().getFileId())));
|
||||
final int aliasUse = importer.indexOf("aid();");
|
||||
assertTrue(renamed.sites().stream().noneMatch(site ->
|
||||
site.span().getFileId().equals(importerAst.span().getFileId())
|
||||
&& site.span().getStart() == aliasUse));
|
||||
}
|
||||
|
||||
private PbsEditorialResolvedSymbol requireHover(
|
||||
final String source,
|
||||
final PbsAst.File ast,
|
||||
|
||||
+28
@@ -110,4 +110,32 @@ public interface FrontendLanguageService {
|
||||
final FrontendEditorialContext editorialContext) {
|
||||
return workspaceSymbols(request);
|
||||
}
|
||||
|
||||
default Optional<FrontendDefinitionLocation> prepareRename(
|
||||
final FrontendDocumentRequest request,
|
||||
final int offset) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<FrontendDefinitionLocation> prepareRename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset) {
|
||||
return prepareRename(request, offset);
|
||||
}
|
||||
|
||||
default FrontendRenameResult rename(
|
||||
final FrontendDocumentRequest request,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
return FrontendRenameResult.unsupported();
|
||||
}
|
||||
|
||||
default FrontendRenameResult rename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset,
|
||||
final String newName) {
|
||||
return rename(request, offset, newName);
|
||||
}
|
||||
}
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FrontendRenameEdit(
|
||||
Path path,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
String newText) {
|
||||
|
||||
public FrontendRenameEdit {
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
if (startOffset < 0) {
|
||||
throw new IllegalArgumentException("startOffset must not be negative");
|
||||
}
|
||||
if (endOffset < startOffset) {
|
||||
throw new IllegalArgumentException("endOffset must not be before startOffset");
|
||||
}
|
||||
newText = Objects.requireNonNull(newText, "newText");
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FrontendRenameResult(
|
||||
Status status,
|
||||
String message,
|
||||
List<FrontendRenameEdit> edits) {
|
||||
|
||||
public enum Status {
|
||||
UNSUPPORTED,
|
||||
REFUSED,
|
||||
APPLIED
|
||||
}
|
||||
|
||||
public FrontendRenameResult {
|
||||
status = Objects.requireNonNull(status, "status");
|
||||
message = message == null ? "" : message.trim();
|
||||
edits = edits == null ? List.of() : List.copyOf(edits);
|
||||
if (status == Status.APPLIED && edits.isEmpty()) {
|
||||
throw new IllegalArgumentException("applied rename must include at least one edit");
|
||||
}
|
||||
if (status != Status.APPLIED && !edits.isEmpty()) {
|
||||
throw new IllegalArgumentException("refused or unsupported rename must not include edits");
|
||||
}
|
||||
}
|
||||
|
||||
public static FrontendRenameResult unsupported() {
|
||||
return new FrontendRenameResult(Status.UNSUPPORTED, "unsupported", List.of());
|
||||
}
|
||||
|
||||
public static FrontendRenameResult refused(final String message) {
|
||||
final String reason = message == null || message.isBlank() ? "not renameable" : message.trim();
|
||||
return new FrontendRenameResult(Status.REFUSED, reason, List.of());
|
||||
}
|
||||
|
||||
public static FrontendRenameResult applied(final List<FrontendRenameEdit> edits) {
|
||||
return new FrontendRenameResult(Status.APPLIED, "", edits);
|
||||
}
|
||||
|
||||
public boolean applied() {
|
||||
return status == Status.APPLIED;
|
||||
}
|
||||
}
|
||||
+3
@@ -27,6 +27,9 @@ class FrontendLanguageServiceTest {
|
||||
assertTrue(service.documentSymbols(request).isEmpty());
|
||||
assertTrue(service.workspaceSymbols(new FrontendWorkspaceRequest(Path.of("."), "frame")).isEmpty());
|
||||
assertTrue(service.prepareEditorialContext(null).isEmpty());
|
||||
assertTrue(service.prepareRename(request, 0).isEmpty());
|
||||
assertEquals(FrontendRenameResult.Status.UNSUPPORTED, service.rename(request, 0, "renamed").status());
|
||||
assertTrue(service.rename(request, 0, "renamed").edits().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user