-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements Rename
provider
#314
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
15f39a4
move conversion to Java types out from `goTo`
simerplaha f27136f
Implement `RenameProvider`
simerplaha d811181
Enable `RenameProvider` capability
simerplaha aefa1d1
perform a cancel check after the search result and before converting …
simerplaha 805056f
Split converters to `RenameConverter` and `CommonConverter`
simerplaha 1406280
Mention `rename request`
simerplaha 08b49a5
replace `toList`
simerplaha 2b12423
better names
simerplaha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
presentation-compiler/src/main/scala/org/alephium/ralph/lsp/pc/search/rename/RenameAll.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see http://www.gnu.org/licenses/. | ||
|
||
package org.alephium.ralph.lsp.pc.search.rename | ||
|
||
import org.alephium.ralph.lsp.pc.log.{ClientLogger, StrictImplicitLogging} | ||
import org.alephium.ralph.lsp.pc.search.CodeProvider | ||
import org.alephium.ralph.lsp.pc.sourcecode.{SourceLocation, SourceCodeState} | ||
import org.alephium.ralph.lsp.pc.util.URIUtil | ||
import org.alephium.ralph.lsp.pc.workspace.WorkspaceState | ||
import org.alephium.ralph.lsp.pc.workspace.build.BuildState | ||
|
||
/** Rename all references */ | ||
private object RenameAll extends StrictImplicitLogging { | ||
|
||
/** | ||
* Searches for related tokens that can be renamed for the token at the given cursor index. | ||
* | ||
* @param cursorIndex The index where this operation is performed. | ||
* @param sourceCode The parsed state of the source-code where the search is executed. | ||
* @param workspace The workspace state where the source-code is located. | ||
* @return Source locations of the tokens to be renamed. | ||
*/ | ||
def rename( | ||
cursorIndex: Int, | ||
sourceCode: SourceCodeState.Parsed, | ||
workspace: WorkspaceState.IsSourceAware | ||
)(implicit logger: ClientLogger): Iterator[SourceLocation.Rename] = { | ||
val result = | ||
CodeProvider | ||
.goToReferences | ||
.search( | ||
cursorIndex = cursorIndex, | ||
sourceCode = sourceCode, | ||
workspace = workspace, | ||
searchSettings = true // isIncludeDeclaration = true | ||
) | ||
.toList // to allow multiple iterations | ||
|
||
// Changes must be within the developer's workspace. Cannot change dependencies. | ||
val cannotRenameCode = | ||
result filter { | ||
ref => | ||
isRenamingDisallowed( | ||
ref = ref, | ||
build = workspace.build | ||
) | ||
} | ||
|
||
if (cannotRenameCode.isEmpty) { | ||
result.iterator | ||
} else { | ||
// contains tokens that cannot be renamed | ||
val outsideCodeURIs = cannotRenameCode.map(_.parsed.fileURI) | ||
val outsideCodeURIStrings = outsideCodeURIs.mkString(", ") | ||
logger.info(s"Operation blocked: Renaming within files outside the active workspace is not allowed. Affected files: $outsideCodeURIStrings") | ||
Iterator.empty | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given go-to reference cannot be renamed. | ||
* | ||
* @param ref The reference to check for remaining restrictions. | ||
* @param build The current workspace build. | ||
* @return True if renaming is disallowed, false otherwise. | ||
*/ | ||
private def isRenamingDisallowed( | ||
ref: SourceLocation.GoToRef, | ||
build: BuildState.Compiled): Boolean = { | ||
val isOutsideWorkspace = | ||
!URIUtil.contains( | ||
parent = build.workspaceURI, | ||
child = ref.parsed.fileURI | ||
) | ||
|
||
val isInDependencyPath = | ||
URIUtil.contains( | ||
parent = build.dependencyPath, | ||
child = ref.parsed.fileURI | ||
) | ||
|
||
isOutsideWorkspace || isInDependencyPath | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...tion-compiler/src/main/scala/org/alephium/ralph/lsp/pc/search/rename/RenameProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see http://www.gnu.org/licenses/. | ||
|
||
package org.alephium.ralph.lsp.pc.search.rename | ||
|
||
import org.alephium.ralph.lsp.pc.log.ClientLogger | ||
import org.alephium.ralph.lsp.pc.search.CodeProvider | ||
import org.alephium.ralph.lsp.pc.sourcecode.{SourceLocation, SourceCodeState} | ||
import org.alephium.ralph.lsp.pc.workspace.WorkspaceState | ||
|
||
/** | ||
* Implements [[CodeProvider]] that provides renaming results of type [[SourceLocation.Rename]]. | ||
*/ | ||
case object RenameProvider extends CodeProvider[Unit, SourceLocation.Rename] { | ||
|
||
/** @inheritdoc */ | ||
override def search( | ||
cursorIndex: Int, | ||
sourceCode: SourceCodeState.Parsed, | ||
workspace: WorkspaceState.IsSourceAware, | ||
searchSettings: Unit | ||
)(implicit logger: ClientLogger): Iterator[SourceLocation.Rename] = | ||
RenameAll.rename( | ||
cursorIndex = cursorIndex, | ||
sourceCode = sourceCode, | ||
workspace = workspace | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it sounds weird that
GoToRef
extendsRename
, is it expected?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The only reason for this is to save a transformation because
rename
returns the same result type asGoToRef
. HavingGoToRef
as a subtype ofRename
means we can return theGoToRef
results fromrename
immediately without the need to transform them to theGoTo.Rename
type.If in the future the
Rename
result types needs additional types, then the transformation will be applied and we can seperate them:If you prefer, I can seperate them now? I was just saving it from doing a transformation, which really isn't that expensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok let's keep it like this for now then.