1 /**
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at*
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0*
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <algorithm>
16 #include <cassert>
17 #include <cstddef>
18 #include <ctime>
19 #include <string>
20
21 #include "find_rename_locations.h"
22 #include "find_references.h"
23
24 namespace ark::es2panda::lsp {
25
FindRenameLocations(CancellationToken * tkn,const std::vector<ark::es2panda::SourceFile> & files,const ark::es2panda::SourceFile & file,size_t position)26 std::set<RenameLocation> FindRenameLocations(CancellationToken *tkn,
27 const std::vector<ark::es2panda::SourceFile> &files,
28 const ark::es2panda::SourceFile &file, size_t position)
29 {
30 auto references = FindReferences(tkn, files, file, position);
31 std::set<RenameLocation> res;
32
33 for (auto ref : references) {
34 auto srcIt = std::find_if(files.begin(), files.end(), [&ref](const SourceFile ¤tFile) {
35 return currentFile.filePath == ref.filePath;
36 });
37 if (srcIt == files.end()) {
38 std::cout << "Error: Could not find " << ref.filePath << " in list!\n";
39 continue;
40 }
41 std::string source = std::string {srcIt->source};
42 // Get prefix and suffix texts
43 std::string prefix;
44 {
45 auto end = source.begin() + ref.start;
46 auto beg = end;
47 while (beg > source.begin() && *(beg - 1) != '\n') {
48 --beg;
49 }
50 prefix = std::string {beg, end};
51 }
52 // Suffix
53 std::string suffix;
54 {
55 auto beg = source.begin() + ref.end;
56 auto end = beg;
57 while (end < source.end() && *end != '\n') {
58 ++end;
59 }
60 suffix = std::string {beg, end};
61 }
62 res.insert(RenameLocation {ref.filePath, ref.start, ref.end, ref.line, prefix, suffix});
63 }
64
65 return res;
66 }
67
FindRenameLocations(const std::vector<ark::es2panda::SourceFile> & files,const ark::es2panda::SourceFile & file,size_t position)68 std::set<RenameLocation> FindRenameLocations(const std::vector<ark::es2panda::SourceFile> &files,
69 const ark::es2panda::SourceFile &file, size_t position)
70 {
71 time_t tmp = 0;
72 CancellationToken cancellationToken {tmp, nullptr};
73 return FindRenameLocations(&cancellationToken, files, file, position);
74 }
75 } // namespace ark::es2panda::lsp