mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[SourceKit] Merge RenameLoc and RenameLocation
It’s easier to understand the code if we don’t have these two nearly, but not quite, identical types.
This commit is contained in:
@@ -10,14 +10,15 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#ifndef SWIFT_IDE_REFACTORING_H
|
#ifndef SWIFT_REFACTORING_REFACTORING_H
|
||||||
#define SWIFT_IDE_REFACTORING_H
|
#define SWIFT_REFACTORING_REFACTORING_H
|
||||||
|
|
||||||
#include "swift/AST/DiagnosticConsumer.h"
|
#include "swift/AST/DiagnosticConsumer.h"
|
||||||
#include "swift/Basic/LLVM.h"
|
#include "swift/Basic/LLVM.h"
|
||||||
#include "swift/Basic/StringExtras.h"
|
#include "swift/Basic/StringExtras.h"
|
||||||
#include "swift/IDE/CancellableResult.h"
|
#include "swift/IDE/CancellableResult.h"
|
||||||
#include "swift/IDE/Utils.h"
|
#include "swift/IDE/Utils.h"
|
||||||
|
#include "swift/Refactoring/RenameLoc.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
|
||||||
namespace swift {
|
namespace swift {
|
||||||
@@ -61,21 +62,6 @@ struct RenameInfo {
|
|||||||
|
|
||||||
llvm::Optional<RenameInfo> getRenameInfo(ResolvedCursorInfoPtr cursorInfo);
|
llvm::Optional<RenameInfo> getRenameInfo(ResolvedCursorInfoPtr cursorInfo);
|
||||||
|
|
||||||
enum class NameUsage {
|
|
||||||
Unknown,
|
|
||||||
Reference,
|
|
||||||
Definition,
|
|
||||||
Call
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenameLoc {
|
|
||||||
unsigned Line;
|
|
||||||
unsigned Column;
|
|
||||||
NameUsage Usage;
|
|
||||||
StringRef OldName;
|
|
||||||
const bool IsFunctionLike;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// An array of \c RenameLoc that also keeps the underlying string storage of
|
/// An array of \c RenameLoc that also keeps the underlying string storage of
|
||||||
/// the \c StringRef inside the \c RenameLoc alive.
|
/// the \c StringRef inside the \c RenameLoc alive.
|
||||||
class RenameLocs {
|
class RenameLocs {
|
||||||
@@ -184,4 +170,4 @@ collectRefactorings(ResolvedCursorInfoPtr CursorInfo, bool ExcludeRename);
|
|||||||
} // namespace ide
|
} // namespace ide
|
||||||
} // namespace swift
|
} // namespace swift
|
||||||
|
|
||||||
#endif // SWIFT_IDE_REFACTORING_H
|
#endif // SWIFT_REFACTORING_REFACTORING_H
|
||||||
|
|||||||
86
include/swift/Refactoring/RenameLoc.h
Normal file
86
include/swift/Refactoring/RenameLoc.h
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift.org open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
||||||
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||||
|
//
|
||||||
|
// See https://swift.org/LICENSE.txt for license information
|
||||||
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef SWIFT_REFACTORING_RENAMELOC_H
|
||||||
|
#define SWIFT_REFACTORING_RENAMELOC_H
|
||||||
|
|
||||||
|
#include "swift/Basic/LLVM.h"
|
||||||
|
|
||||||
|
namespace swift {
|
||||||
|
namespace ide {
|
||||||
|
|
||||||
|
/// Describes how a `ResolvedLoc` is being used
|
||||||
|
enum class RenameLocUsage {
|
||||||
|
/// The definition of a function/subscript/variable/...
|
||||||
|
Definition,
|
||||||
|
|
||||||
|
/// The symbol is being referenced.
|
||||||
|
///
|
||||||
|
/// This includes
|
||||||
|
/// - References to variables
|
||||||
|
/// - Unapplied references to functions (`myStruct.memberFunc`)
|
||||||
|
/// - Calls to subscripts (`myArray[1]`, location is `[` here, length 1)
|
||||||
|
Reference,
|
||||||
|
|
||||||
|
/// A function that is being called.
|
||||||
|
Call,
|
||||||
|
|
||||||
|
/// Unknown name usage occurs if we don't have an entry in the index that
|
||||||
|
/// tells us whether the location is a call, reference or a definition. The
|
||||||
|
/// most common reasons why this happens is if the editor is adding syntactic
|
||||||
|
/// results (eg. from comments or string literals).
|
||||||
|
Unknown
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The input to `findSyntacticRenameRanges`.
|
||||||
|
///
|
||||||
|
/// Specifies the location of a base name for which `findSyntacticRenameRanges`
|
||||||
|
/// should find the argument labels as well as some semantic information needed
|
||||||
|
/// to resolve the rename ranges.
|
||||||
|
struct RenameLoc {
|
||||||
|
/// The line at which the base name starts (1-based).
|
||||||
|
unsigned Line;
|
||||||
|
|
||||||
|
/// The column at which the base name (excluding trivia) starts (1-based).
|
||||||
|
unsigned Column;
|
||||||
|
|
||||||
|
/// /// The offset at which the related name starts.
|
||||||
|
unsigned Offset;
|
||||||
|
|
||||||
|
/// The length of the base name in the related identifier. For functions,
|
||||||
|
/// this does not include the parameters/arguments.
|
||||||
|
unsigned Length;
|
||||||
|
|
||||||
|
/// How the identifier is being used (call, reference, definition, unknown).
|
||||||
|
///
|
||||||
|
/// Used to decide whether a given occurance should be renamed and/or if its
|
||||||
|
/// argument labels should be renamed.
|
||||||
|
RenameLocUsage Usage;
|
||||||
|
|
||||||
|
/// The old decl name being renamed.
|
||||||
|
///
|
||||||
|
/// ### Examples
|
||||||
|
/// - `foo(a:b:)` for a function with two parameters.
|
||||||
|
/// - `foo` for a variable.
|
||||||
|
/// - `foo(_:)` for a function with a single unnamed parameter
|
||||||
|
/// - `foo()` for a function without parameters.
|
||||||
|
StringRef OldName;
|
||||||
|
|
||||||
|
RenameLoc(unsigned Line, unsigned Column, RenameLocUsage Usage,
|
||||||
|
StringRef OldName)
|
||||||
|
: Line(Line), Column(Column), Usage(Usage), OldName(OldName) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ide
|
||||||
|
} // namespace swift
|
||||||
|
|
||||||
|
#endif // SWIFT_REFACTORING_RENAMELOC_H
|
||||||
@@ -77,10 +77,8 @@ static SourceLoc getNewFuncInsertLoc(DeclContext *DC,
|
|||||||
return SourceLoc();
|
return SourceLoc();
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<NoteRegion> getNotableRegions(StringRef SourceText,
|
static std::vector<NoteRegion>
|
||||||
unsigned NameOffset,
|
getNotableRegions(StringRef SourceText, unsigned NameOffset, StringRef Name) {
|
||||||
StringRef Name,
|
|
||||||
bool IsFunctionLike = false) {
|
|
||||||
auto InputBuffer =
|
auto InputBuffer =
|
||||||
llvm::MemoryBuffer::getMemBufferCopy(SourceText, "<extract>");
|
llvm::MemoryBuffer::getMemBufferCopy(SourceText, "<extract>");
|
||||||
|
|
||||||
@@ -106,8 +104,7 @@ static std::vector<NoteRegion> getNotableRegions(StringRef SourceText,
|
|||||||
assert(!Resolved.empty() && "Failed to resolve generated func name loc");
|
assert(!Resolved.empty() && "Failed to resolve generated func name loc");
|
||||||
|
|
||||||
RenameLoc RenameConfig = {LineAndCol.first, LineAndCol.second,
|
RenameLoc RenameConfig = {LineAndCol.first, LineAndCol.second,
|
||||||
NameUsage::Definition, /*OldName=*/Name,
|
RenameLocUsage::Definition, /*OldName=*/Name};
|
||||||
IsFunctionLike};
|
|
||||||
std::vector<RenameRangeDetail> Ranges =
|
std::vector<RenameRangeDetail> Ranges =
|
||||||
getSyntacticRenameRangeDetails(SM, Name, Resolved.back(), RenameConfig)
|
getSyntacticRenameRangeDetails(SM, Name, Resolved.back(), RenameConfig)
|
||||||
.Ranges;
|
.Ranges;
|
||||||
@@ -290,13 +287,11 @@ bool RefactoringActionExtractFunction::performChange() {
|
|||||||
|
|
||||||
StringRef DeclStr(Buffer.begin() + FuncBegin, FuncEnd - FuncBegin);
|
StringRef DeclStr(Buffer.begin() + FuncBegin, FuncEnd - FuncBegin);
|
||||||
auto NotableFuncRegions =
|
auto NotableFuncRegions =
|
||||||
getNotableRegions(DeclStr, FuncNameOffset, ExtractedFuncName,
|
getNotableRegions(DeclStr, FuncNameOffset, ExtractedFuncName);
|
||||||
/*IsFunctionLike=*/true);
|
|
||||||
|
|
||||||
StringRef CallStr(Buffer.begin() + ReplaceBegin, ReplaceEnd - ReplaceBegin);
|
StringRef CallStr(Buffer.begin() + ReplaceBegin, ReplaceEnd - ReplaceBegin);
|
||||||
auto NotableCallRegions =
|
auto NotableCallRegions =
|
||||||
getNotableRegions(CallStr, CallNameOffset, ExtractedFuncName,
|
getNotableRegions(CallStr, CallNameOffset, ExtractedFuncName);
|
||||||
/*IsFunctionLike=*/true);
|
|
||||||
|
|
||||||
// Insert the new function's declaration.
|
// Insert the new function's declaration.
|
||||||
EditConsumer.accept(SM, InsertLoc, DeclStr, NotableFuncRegions);
|
EditConsumer.accept(SM, InsertLoc, DeclStr, NotableFuncRegions);
|
||||||
|
|||||||
@@ -219,7 +219,6 @@ private:
|
|||||||
locations.push_back(std::move(*loc));
|
locations.push_back(std::move(*loc));
|
||||||
} else {
|
} else {
|
||||||
assert(existingLoc->OldName == loc->OldName &&
|
assert(existingLoc->OldName == loc->OldName &&
|
||||||
existingLoc->IsFunctionLike == loc->IsFunctionLike &&
|
|
||||||
"Asked to do a different rename for the same location?");
|
"Asked to do a different rename for the same location?");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,37 +240,19 @@ RenameRangeCollector::indexSymbolToRenameLoc(const index::IndexSymbol &symbol) {
|
|||||||
return llvm::None;
|
return llvm::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
NameUsage usage = NameUsage::Unknown;
|
RenameLocUsage usage = RenameLocUsage::Unknown;
|
||||||
if (symbol.roles & (unsigned)index::SymbolRole::Call) {
|
if (symbol.roles & (unsigned)index::SymbolRole::Call) {
|
||||||
usage = NameUsage::Call;
|
usage = RenameLocUsage::Call;
|
||||||
} else if (symbol.roles & (unsigned)index::SymbolRole::Definition) {
|
} else if (symbol.roles & (unsigned)index::SymbolRole::Definition) {
|
||||||
usage = NameUsage::Definition;
|
usage = RenameLocUsage::Definition;
|
||||||
} else if (symbol.roles & (unsigned)index::SymbolRole::Reference) {
|
} else if (symbol.roles & (unsigned)index::SymbolRole::Reference) {
|
||||||
usage = NameUsage::Reference;
|
usage = RenameLocUsage::Reference;
|
||||||
} else {
|
} else {
|
||||||
llvm_unreachable("unexpected role");
|
llvm_unreachable("unexpected role");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isFunctionLike = false;
|
|
||||||
|
|
||||||
switch (symbol.symInfo.Kind) {
|
|
||||||
case index::SymbolKind::EnumConstant:
|
|
||||||
case index::SymbolKind::Function:
|
|
||||||
case index::SymbolKind::Constructor:
|
|
||||||
case index::SymbolKind::ConversionFunction:
|
|
||||||
case index::SymbolKind::InstanceMethod:
|
|
||||||
case index::SymbolKind::ClassMethod:
|
|
||||||
case index::SymbolKind::StaticMethod:
|
|
||||||
isFunctionLike = true;
|
|
||||||
break;
|
|
||||||
case index::SymbolKind::Class:
|
|
||||||
case index::SymbolKind::Enum:
|
|
||||||
case index::SymbolKind::Struct:
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
StringRef oldName = stringStorage->copyString(symbol.name);
|
StringRef oldName = stringStorage->copyString(symbol.name);
|
||||||
return RenameLoc{symbol.line, symbol.column, usage, oldName, isFunctionLike};
|
return RenameLoc{symbol.line, symbol.column, usage, oldName};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the decl context that we need to walk when renaming \p VD.
|
/// Get the decl context that we need to walk when renaming \p VD.
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RenameLoc.Usage == NameUsage::Call && !RenameLoc.IsFunctionLike) {
|
if (RenameLoc.Usage == RenameLocUsage::Call && !OldName.isFunction()) {
|
||||||
Diags.diagnose(Location, diag::name_not_functionlike, NewName);
|
Diags.diagnose(Location, diag::name_not_functionlike, NewName);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -396,13 +396,13 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
if (!resolved.range.isValid())
|
if (!resolved.range.isValid())
|
||||||
return RegionType::Unmatched;
|
return RegionType::Unmatched;
|
||||||
|
|
||||||
NameUsage usage = config.Usage;
|
RenameLocUsage usage = config.Usage;
|
||||||
|
|
||||||
auto regionKind = getSyntacticRenameRegionType(resolved);
|
auto regionKind = getSyntacticRenameRegionType(resolved);
|
||||||
|
|
||||||
SpecialBaseName specialBaseName = specialBaseNameFor(Old);
|
SpecialBaseName specialBaseName = specialBaseNameFor(Old);
|
||||||
|
|
||||||
if (usage == NameUsage::Unknown) {
|
if (usage == RenameLocUsage::Unknown) {
|
||||||
// Unknown name usage occurs if we don't have an entry in the index that
|
// Unknown name usage occurs if we don't have an entry in the index that
|
||||||
// tells us whether the location is a call, reference or a definition. The
|
// tells us whether the location is a call, reference or a definition. The
|
||||||
// most common reasons why this happens is if the editor is adding syntactic
|
// most common reasons why this happens is if the editor is adding syntactic
|
||||||
@@ -440,7 +440,8 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
// be called as `myStruct()`, so even if the base fails to be renamed,
|
// be called as `myStruct()`, so even if the base fails to be renamed,
|
||||||
// continue.
|
// continue.
|
||||||
// But the names do need to match for definitions and references.
|
// But the names do need to match for definitions and references.
|
||||||
if (usage == NameUsage::Definition || usage == NameUsage::Reference) {
|
if (usage == RenameLocUsage::Definition ||
|
||||||
|
usage == RenameLocUsage::Reference) {
|
||||||
return RegionType::Mismatch;
|
return RegionType::Mismatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -450,7 +451,7 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
// Accesses to the subscript are modelled as references with `[` as the
|
// Accesses to the subscript are modelled as references with `[` as the
|
||||||
// base name, which does not match. Subscripts are never called in the
|
// base name, which does not match. Subscripts are never called in the
|
||||||
// index.
|
// index.
|
||||||
if (usage == NameUsage::Definition) {
|
if (usage == RenameLocUsage::Definition) {
|
||||||
if (renameBase(resolved.range, RefactoringRangeKind::KeywordBaseName)) {
|
if (renameBase(resolved.range, RefactoringRangeKind::KeywordBaseName)) {
|
||||||
return RegionType::Mismatch;
|
return RegionType::Mismatch;
|
||||||
}
|
}
|
||||||
@@ -462,18 +463,18 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
bool isCallSite = false;
|
bool isCallSite = false;
|
||||||
if (Old.isFunction()) {
|
if (Old.isFunction()) {
|
||||||
switch (usage) {
|
switch (usage) {
|
||||||
case NameUsage::Call:
|
case RenameLocUsage::Call:
|
||||||
// All calls except for operators have argument labels that should be
|
// All calls except for operators have argument labels that should be
|
||||||
// renamed.
|
// renamed.
|
||||||
handleLabels = !Lexer::isOperator(Old.base());
|
handleLabels = !Lexer::isOperator(Old.base());
|
||||||
isCallSite = true;
|
isCallSite = true;
|
||||||
break;
|
break;
|
||||||
case NameUsage::Definition:
|
case RenameLocUsage::Definition:
|
||||||
// All function definitions have argument labels that should be renamed.
|
// All function definitions have argument labels that should be renamed.
|
||||||
handleLabels = true;
|
handleLabels = true;
|
||||||
isCallSite = false;
|
isCallSite = false;
|
||||||
break;
|
break;
|
||||||
case NameUsage::Reference:
|
case RenameLocUsage::Reference:
|
||||||
if (resolved.labelType == LabelRangeType::CompoundName) {
|
if (resolved.labelType == LabelRangeType::CompoundName) {
|
||||||
// If we have a compound name that specifies argument labels to
|
// If we have a compound name that specifies argument labels to
|
||||||
// disambiguate functions with the same base name, we always need to
|
// disambiguate functions with the same base name, we always need to
|
||||||
@@ -491,7 +492,7 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
isCallSite = false;
|
isCallSite = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NameUsage::Unknown:
|
case RenameLocUsage::Unknown:
|
||||||
// If we don't know where the function is used, fall back to trying to
|
// If we don't know where the function is used, fall back to trying to
|
||||||
// rename labels if there are some.
|
// rename labels if there are some.
|
||||||
handleLabels = resolved.labelType != LabelRangeType::None;
|
handleLabels = resolved.labelType != LabelRangeType::None;
|
||||||
@@ -505,8 +506,8 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
|
|||||||
renameLabels(resolved.labelRanges, resolved.firstTrailingLabel,
|
renameLabels(resolved.labelRanges, resolved.firstTrailingLabel,
|
||||||
resolved.labelType, isCallSite);
|
resolved.labelType, isCallSite);
|
||||||
if (renameLabelsFailed) {
|
if (renameLabelsFailed) {
|
||||||
return usage == NameUsage::Unknown ? RegionType::Unmatched
|
return usage == RenameLocUsage::Unknown ? RegionType::Unmatched
|
||||||
: RegionType::Mismatch;
|
: RegionType::Mismatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#include "swift/AST/Type.h"
|
#include "swift/AST/Type.h"
|
||||||
#include "swift/IDE/CancellableResult.h"
|
#include "swift/IDE/CancellableResult.h"
|
||||||
#include "swift/IDE/CodeCompletionResult.h"
|
#include "swift/IDE/CodeCompletionResult.h"
|
||||||
|
#include "swift/Refactoring/RenameLoc.h"
|
||||||
#include "llvm/ADT/ArrayRef.h"
|
#include "llvm/ADT/ArrayRef.h"
|
||||||
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
||||||
#include "llvm/ADT/Optional.h"
|
#include "llvm/ADT/Optional.h"
|
||||||
@@ -37,6 +38,7 @@ namespace llvm {
|
|||||||
namespace SourceKit {
|
namespace SourceKit {
|
||||||
class GlobalConfig;
|
class GlobalConfig;
|
||||||
using swift::ide::CancellableResult;
|
using swift::ide::CancellableResult;
|
||||||
|
using swift::ide::RenameLoc;
|
||||||
|
|
||||||
struct EntityInfo {
|
struct EntityInfo {
|
||||||
UIdent Kind;
|
UIdent Kind;
|
||||||
@@ -893,25 +895,6 @@ struct CategorizedRenameRanges {
|
|||||||
std::vector<RenameRangeDetail> Ranges;
|
std::vector<RenameRangeDetail> Ranges;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class RenameType {
|
|
||||||
Unknown,
|
|
||||||
Definition,
|
|
||||||
Reference,
|
|
||||||
Call
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenameLocation {
|
|
||||||
unsigned Line;
|
|
||||||
unsigned Column;
|
|
||||||
RenameType Type;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenameLocations {
|
|
||||||
StringRef OldName;
|
|
||||||
const bool IsFunctionLike;
|
|
||||||
std::vector<RenameLocation> LineColumnLocs;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct IndexStoreOptions {
|
struct IndexStoreOptions {
|
||||||
std::string IndexStorePath;
|
std::string IndexStorePath;
|
||||||
std::string IndexUnitOutputPath;
|
std::string IndexUnitOutputPath;
|
||||||
@@ -1192,7 +1175,7 @@ public:
|
|||||||
|
|
||||||
virtual CancellableResult<std::vector<CategorizedRenameRanges>>
|
virtual CancellableResult<std::vector<CategorizedRenameRanges>>
|
||||||
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
||||||
ArrayRef<RenameLocations> RenameLocations,
|
ArrayRef<RenameLoc> RenameLocations,
|
||||||
ArrayRef<const char *> Args) = 0;
|
ArrayRef<const char *> Args) = 0;
|
||||||
virtual void
|
virtual void
|
||||||
findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
|
findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
|
||||||
|
|||||||
@@ -1344,22 +1344,6 @@ void RequestRefactoringEditConsumer::handleDiagnostic(
|
|||||||
Impl.DiagConsumer.handleDiagnostic(SM, Info);
|
Impl.DiagConsumer.handleDiagnostic(SM, Info);
|
||||||
}
|
}
|
||||||
|
|
||||||
static NameUsage getNameUsage(RenameType Type) {
|
|
||||||
switch (Type) {
|
|
||||||
case RenameType::Definition:
|
|
||||||
return NameUsage::Definition;
|
|
||||||
case RenameType::Reference:
|
|
||||||
return NameUsage::Reference;
|
|
||||||
case RenameType::Call:
|
|
||||||
return NameUsage::Call;
|
|
||||||
case RenameType::Unknown:
|
|
||||||
return NameUsage::Unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::vector<RenameLoc>
|
|
||||||
getSyntacticRenameLocs(ArrayRef<RenameLocations> RenameLocations);
|
|
||||||
|
|
||||||
/// Translates a vector of \c SyntacticRenameRangeDetails to a vector of
|
/// Translates a vector of \c SyntacticRenameRangeDetails to a vector of
|
||||||
/// \c CategorizedRenameRanges.
|
/// \c CategorizedRenameRanges.
|
||||||
static std::vector<CategorizedRenameRanges> getCategorizedRenameRanges(
|
static std::vector<CategorizedRenameRanges> getCategorizedRenameRanges(
|
||||||
@@ -1390,7 +1374,7 @@ static std::vector<CategorizedRenameRanges> getCategorizedRenameRanges(
|
|||||||
|
|
||||||
CancellableResult<std::vector<CategorizedRenameRanges>>
|
CancellableResult<std::vector<CategorizedRenameRanges>>
|
||||||
SwiftLangSupport::findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
SwiftLangSupport::findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
||||||
ArrayRef<RenameLocations> RenameLocations,
|
ArrayRef<RenameLoc> RenameLocs,
|
||||||
ArrayRef<const char *> Args) {
|
ArrayRef<const char *> Args) {
|
||||||
using ResultType = CancellableResult<std::vector<CategorizedRenameRanges>>;
|
using ResultType = CancellableResult<std::vector<CategorizedRenameRanges>>;
|
||||||
std::string Error;
|
std::string Error;
|
||||||
@@ -1402,7 +1386,6 @@ SwiftLangSupport::findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
|||||||
return ResultType::failure(Error);
|
return ResultType::failure(Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto RenameLocs = getSyntacticRenameLocs(RenameLocations);
|
|
||||||
auto SyntacticRenameRanges =
|
auto SyntacticRenameRanges =
|
||||||
swift::ide::findSyntacticRenameRanges(SF, RenameLocs,
|
swift::ide::findSyntacticRenameRanges(SF, RenameLocs,
|
||||||
/*NewName=*/StringRef());
|
/*NewName=*/StringRef());
|
||||||
@@ -1503,19 +1486,6 @@ SourceFile *SwiftLangSupport::getSyntacticSourceFile(
|
|||||||
return SF;
|
return SF;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<RenameLoc>
|
|
||||||
getSyntacticRenameLocs(ArrayRef<RenameLocations> RenameLocations) {
|
|
||||||
std::vector<RenameLoc> RenameLocs;
|
|
||||||
for(const auto &Locations: RenameLocations) {
|
|
||||||
for(const auto &Location: Locations.LineColumnLocs) {
|
|
||||||
RenameLocs.push_back({Location.Line, Location.Column,
|
|
||||||
getNameUsage(Location.Type), Locations.OldName,
|
|
||||||
Locations.IsFunctionLike});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RenameLocs;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf,
|
void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf,
|
||||||
StringRef ModuleName,
|
StringRef ModuleName,
|
||||||
ArrayRef<const char *> Args,
|
ArrayRef<const char *> Args,
|
||||||
|
|||||||
@@ -695,7 +695,7 @@ public:
|
|||||||
|
|
||||||
CancellableResult<std::vector<CategorizedRenameRanges>>
|
CancellableResult<std::vector<CategorizedRenameRanges>>
|
||||||
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
||||||
ArrayRef<RenameLocations> RenameLocations,
|
ArrayRef<RenameLoc> RenameLocations,
|
||||||
ArrayRef<const char *> Args) override;
|
ArrayRef<const char *> Args) override;
|
||||||
|
|
||||||
void findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
|
void findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ editorFindModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args);
|
|||||||
|
|
||||||
static bool
|
static bool
|
||||||
buildRenameLocationsFromDict(const RequestDict &Req,
|
buildRenameLocationsFromDict(const RequestDict &Req,
|
||||||
std::vector<RenameLocations> &RenameLocations,
|
std::vector<RenameLoc> &RenameLocations,
|
||||||
llvm::SmallString<64> &Error);
|
llvm::SmallString<64> &Error);
|
||||||
|
|
||||||
static sourcekitd_response_t
|
static sourcekitd_response_t
|
||||||
@@ -323,7 +323,7 @@ static sourcekitd_response_t createCategorizedRenameRangesResponse(
|
|||||||
|
|
||||||
static sourcekitd_response_t
|
static sourcekitd_response_t
|
||||||
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
||||||
ArrayRef<RenameLocations> RenameLocations,
|
ArrayRef<RenameLoc> RenameLocations,
|
||||||
ArrayRef<const char *> Args);
|
ArrayRef<const char *> Args);
|
||||||
|
|
||||||
static bool isSemanticEditorDisabled();
|
static bool isSemanticEditorDisabled();
|
||||||
@@ -1069,7 +1069,7 @@ handleRequestFindRenameRanges(const RequestDict &Req,
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
SmallString<64> ErrBuf;
|
SmallString<64> ErrBuf;
|
||||||
std::vector<RenameLocations> RenameLocations;
|
std::vector<RenameLoc> RenameLocations;
|
||||||
if (buildRenameLocationsFromDict(Req, RenameLocations, ErrBuf))
|
if (buildRenameLocationsFromDict(Req, RenameLocations, ErrBuf))
|
||||||
return Rec(createErrorRequestFailed(ErrBuf.c_str()));
|
return Rec(createErrorRequestFailed(ErrBuf.c_str()));
|
||||||
return Rec(findRenameRanges(InputBuf.get(), RenameLocations, Args));
|
return Rec(findRenameRanges(InputBuf.get(), RenameLocations, Args));
|
||||||
@@ -3996,25 +3996,16 @@ editorFindModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args) {
|
|||||||
|
|
||||||
static bool
|
static bool
|
||||||
buildRenameLocationsFromDict(const RequestDict &Req,
|
buildRenameLocationsFromDict(const RequestDict &Req,
|
||||||
std::vector<RenameLocations> &RenameLocations,
|
std::vector<RenameLoc> &RenameLocations,
|
||||||
llvm::SmallString<64> &Error) {
|
llvm::SmallString<64> &Error) {
|
||||||
bool Failed = Req.dictionaryArrayApply(KeyRenameLocations,
|
bool Failed = Req.dictionaryArrayApply(KeyRenameLocations,
|
||||||
[&](RequestDict RenameLocation) {
|
[&](RequestDict RenameLocation) {
|
||||||
int64_t IsFunctionLike = false;
|
|
||||||
if (RenameLocation.getInt64(KeyIsFunctionLike, IsFunctionLike, false)) {
|
|
||||||
Error = "missing key.is_function_like";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<StringRef> OldName = RenameLocation.getString(KeyName);
|
Optional<StringRef> OldName = RenameLocation.getString(KeyName);
|
||||||
if (!OldName.has_value()) {
|
if (!OldName.has_value()) {
|
||||||
Error = "missing key.name";
|
Error = "missing key.name";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenameLocations.push_back(
|
|
||||||
{*OldName, static_cast<bool>(IsFunctionLike), {}});
|
|
||||||
auto &LineCols = RenameLocations.back().LineColumnLocs;
|
|
||||||
bool Failed = RenameLocation.dictionaryArrayApply(KeyLocations,
|
bool Failed = RenameLocation.dictionaryArrayApply(KeyLocations,
|
||||||
[&](RequestDict LineAndCol) {
|
[&](RequestDict LineAndCol) {
|
||||||
int64_t Line = 0;
|
int64_t Line = 0;
|
||||||
@@ -4034,19 +4025,21 @@ buildRenameLocationsFromDict(const RequestDict &Req,
|
|||||||
Error = "missing key.nametype";
|
Error = "missing key.nametype";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
RenameType RenameType = RenameType::Unknown;
|
using swift::ide::RenameLocUsage;
|
||||||
|
RenameLocUsage RenameType = RenameLocUsage::Unknown;
|
||||||
if (NameType == KindDefinition) {
|
if (NameType == KindDefinition) {
|
||||||
RenameType = RenameType::Definition;
|
RenameType = RenameLocUsage::Definition;
|
||||||
} else if (NameType == KindReference) {
|
} else if (NameType == KindReference) {
|
||||||
RenameType = RenameType::Reference;
|
RenameType = RenameLocUsage::Reference;
|
||||||
} else if (NameType == KindCall) {
|
} else if (NameType == KindCall) {
|
||||||
RenameType = RenameType::Call;
|
RenameType = RenameLocUsage::Call;
|
||||||
} else if (NameType != KindUnknown) {
|
} else if (NameType != KindUnknown) {
|
||||||
Error = "invalid value for 'key.nametype'";
|
Error = "invalid value for 'key.nametype'";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
LineCols.push_back({static_cast<unsigned>(Line),
|
RenameLocations.emplace_back(
|
||||||
static_cast<unsigned>(Column), RenameType});
|
static_cast<unsigned>(Line), static_cast<unsigned>(Column),
|
||||||
|
RenameType, *OldName);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
if (Failed && Error.empty()) {
|
if (Failed && Error.empty()) {
|
||||||
@@ -4145,7 +4138,7 @@ static sourcekitd_response_t createCategorizedRenameRangesResponse(
|
|||||||
|
|
||||||
static sourcekitd_response_t
|
static sourcekitd_response_t
|
||||||
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
findRenameRanges(llvm::MemoryBuffer *InputBuf,
|
||||||
ArrayRef<RenameLocations> RenameLocations,
|
ArrayRef<RenameLoc> RenameLocations,
|
||||||
ArrayRef<const char *> Args) {
|
ArrayRef<const char *> Args) {
|
||||||
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
|
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
|
||||||
CancellableResult<std::vector<CategorizedRenameRanges>> ReqResult =
|
CancellableResult<std::vector<CategorizedRenameRanges>> ReqResult =
|
||||||
|
|||||||
@@ -176,18 +176,18 @@ bool doesFileExist(StringRef Path) {
|
|||||||
struct RefactorLoc {
|
struct RefactorLoc {
|
||||||
unsigned Line;
|
unsigned Line;
|
||||||
unsigned Column;
|
unsigned Column;
|
||||||
NameUsage Usage;
|
RenameLocUsage Usage;
|
||||||
};
|
};
|
||||||
|
|
||||||
NameUsage convertToNameUsage(StringRef RoleString) {
|
RenameLocUsage convertToNameUsage(StringRef RoleString) {
|
||||||
if (RoleString == "unknown")
|
if (RoleString == "unknown")
|
||||||
return NameUsage::Unknown;
|
return RenameLocUsage::Unknown;
|
||||||
if (RoleString == "def")
|
if (RoleString == "def")
|
||||||
return NameUsage::Definition;
|
return RenameLocUsage::Definition;
|
||||||
if (RoleString == "ref")
|
if (RoleString == "ref")
|
||||||
return NameUsage::Reference;
|
return RenameLocUsage::Reference;
|
||||||
if (RoleString == "call")
|
if (RoleString == "call")
|
||||||
return NameUsage::Call;
|
return RenameLocUsage::Call;
|
||||||
llvm_unreachable("unhandled role string");
|
llvm_unreachable("unhandled role string");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +201,8 @@ std::vector<RefactorLoc> getLocsByLabelOrPosition(StringRef LabelOrLineCol,
|
|||||||
if (LabelOrLineCol.contains(':')) {
|
if (LabelOrLineCol.contains(':')) {
|
||||||
auto LineCol = parseLineCol(LabelOrLineCol);
|
auto LineCol = parseLineCol(LabelOrLineCol);
|
||||||
if (LineCol.has_value()) {
|
if (LineCol.has_value()) {
|
||||||
LocResults.push_back({LineCol.value().first,LineCol.value().second,
|
LocResults.push_back({LineCol.value().first, LineCol.value().second,
|
||||||
NameUsage::Unknown});
|
RenameLocUsage::Unknown});
|
||||||
} else {
|
} else {
|
||||||
llvm::errs() << "cannot parse position pair.";
|
llvm::errs() << "cannot parse position pair.";
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ std::vector<RefactorLoc> getLocsByLabelOrPosition(StringRef LabelOrLineCol,
|
|||||||
unsigned ColumnOffset = 0;
|
unsigned ColumnOffset = 0;
|
||||||
if (Matches[2].length() > 0 && !llvm::to_integer(Matches[2].str(), ColumnOffset))
|
if (Matches[2].length() > 0 && !llvm::to_integer(Matches[2].str(), ColumnOffset))
|
||||||
continue; // bad column offset
|
continue; // bad column offset
|
||||||
auto Usage = NameUsage::Reference;
|
auto Usage = RenameLocUsage::Reference;
|
||||||
if (Matches[3].length() > 0)
|
if (Matches[3].length() > 0)
|
||||||
Usage = convertToNameUsage(Matches[3].str());
|
Usage = convertToNameUsage(Matches[3].str());
|
||||||
LocResults.push_back({Line, Column + ColumnOffset, Usage});
|
LocResults.push_back({Line, Column + ColumnOffset, Usage});
|
||||||
@@ -242,14 +242,12 @@ std::vector<RefactorLoc> getLocsByLabelOrPosition(StringRef LabelOrLineCol,
|
|||||||
|
|
||||||
std::vector<RenameLoc> getRenameLocs(unsigned BufferID, SourceManager &SM,
|
std::vector<RenameLoc> getRenameLocs(unsigned BufferID, SourceManager &SM,
|
||||||
ArrayRef<RefactorLoc> Locs,
|
ArrayRef<RefactorLoc> Locs,
|
||||||
StringRef OldName, StringRef NewName,
|
StringRef OldName, StringRef NewName) {
|
||||||
bool IsFunctionLike) {
|
|
||||||
std::vector<RenameLoc> Renames;
|
std::vector<RenameLoc> Renames;
|
||||||
llvm::transform(
|
llvm::transform(Locs, std::back_inserter(Renames),
|
||||||
Locs, std::back_inserter(Renames),
|
[&](const RefactorLoc &Loc) -> RenameLoc {
|
||||||
[&](const RefactorLoc &Loc) -> RenameLoc {
|
return {Loc.Line, Loc.Column, Loc.Usage, OldName};
|
||||||
return {Loc.Line, Loc.Column, Loc.Usage, OldName, IsFunctionLike};
|
});
|
||||||
});
|
|
||||||
return Renames;
|
return Renames;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,8 +449,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<RenameLoc> RenameLocs =
|
std::vector<RenameLoc> RenameLocs =
|
||||||
getRenameLocs(BufferID, SM, Start, options::OldName, NewName,
|
getRenameLocs(BufferID, SM, Start, options::OldName, NewName);
|
||||||
options::IsFunctionLike.getNumOccurrences());
|
|
||||||
|
|
||||||
if (options::Action != RefactoringKind::FindGlobalRenameRanges) {
|
if (options::Action != RefactoringKind::FindGlobalRenameRanges) {
|
||||||
llvm_unreachable("unexpected refactoring kind");
|
llvm_unreachable("unexpected refactoring kind");
|
||||||
|
|||||||
Reference in New Issue
Block a user