Skip to content
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 8 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.alephium.ralph.lsp.pc.log.ClientLogger
import org.alephium.ralph.lsp.pc.search.completion.{Suggestion, CodeCompletionProvider}
import org.alephium.ralph.lsp.pc.search.gotodef.GoToDefinitionProvider
import org.alephium.ralph.lsp.pc.search.gotoref.GoToReferenceProvider
import org.alephium.ralph.lsp.pc.search.rename.RenameProvider
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, WorkspaceSearcher}
Expand Down Expand Up @@ -68,6 +69,10 @@ object CodeProvider {
implicit val goToReferences: CodeProvider[Boolean, SourceLocation.GoToRef] =
GoToReferenceProvider

/** The rename request implementation of [[CodeProvider]]. */
implicit val renameProvider: CodeProvider[Unit, SourceLocation.Rename] =
RenameProvider

/**
* Execute search at cursor position within the current workspace state.
*
Expand Down
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
}

}
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
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ object SourceLocation {
*/
sealed trait GoToDef extends GoTo

/**
* Result types for renaming location search results.
*/
sealed trait Rename extends GoTo

/**
* Result types for GoTo references location search results.
*/
sealed trait GoToRef extends GoTo
sealed trait GoToRef extends Rename
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it sounds weird that GoToRefextends Rename, is it expected?

Copy link
Member Author

@simerplaha simerplaha Oct 16, 2024

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 as GoToRef. Having GoToRef as a subtype of Rename means we can return the GoToRef results from rename immediately without the need to transform them to the GoTo.Rename type.

If in the future the Rename result types needs additional types, then the transformation will be applied and we can seperate them:

sealed trait Rename  extends GoTo
sealed trait GoToRef extends GoTo

If you prefer, I can seperate them now? I was just saving it from doing a transformation, which really isn't that expensive.

Copy link
Member

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.


/**
* Represents a source file ([[SourceCodeState.Parsed]]) without
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ object URIUtil {
child = Paths.get(child)
)

/** Is the child [[Path]] within the parent [[URI]] */
def contains(
parent: URI,
child: Path): Boolean =
contains(
parent = Paths.get(parent),
child = child
)

/** Is the child [[URI]] within the parent [[Path]] */
def contains(
parent: Path,
child: URI): Boolean =
contains(
parent = parent,
child = Paths.get(child)
)

/**
* Checks if a given path exists or is undefined.
*
Expand Down