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,
|
||||
|
||||
Reference in New Issue
Block a user