[PATCH] D64305: [clangd] Add path mappings functionality

William Wagner via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 5 21:39:43 PST 2020


wwagner19 updated this revision to Diff 236293.
wwagner19 added a comment.

The last diff was broken, this most recent one

- Changes IsIncoming boolean to an enum
- Refactors the matching path logic


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64305/new/

https://reviews.llvm.org/D64305

Files:
  clang-tools-extra/clangd/PathMapping.cpp


Index: clang-tools-extra/clangd/PathMapping.cpp
===================================================================
--- clang-tools-extra/clangd/PathMapping.cpp
+++ clang-tools-extra/clangd/PathMapping.cpp
@@ -128,21 +128,6 @@
                                              llvm::inconvertibleErrorCode());
 }
 
-// Returns whether a path mapping should take place for OrigPath
-// i.e. MappingPath is a proper sub-path of  OrigPath
-bool mappingMatches(llvm::StringRef OrigPath, llvm::StringRef MappingPath) {
-  namespace path = llvm::sys::path;
-  auto OrigPathItr = path::begin(OrigPath, path::Style::posix);
-  auto OrigPathEnd = path::end(OrigPath);
-  auto MappingPathItr = path::begin(MappingPath, path::Style::posix);
-  auto MappingPathEnd = path::end(MappingPath);
-  if (std::distance(OrigPathItr, OrigPathEnd) <
-      std::distance(MappingPathItr, MappingPathEnd)) {
-    return false;
-  }
-  return std::equal(MappingPathItr, MappingPathEnd, OrigPathItr);
-}
-
 // Converts a unix/windows path to the path portion of a file URI
 // e.g. "C:\foo" -> "/C:/foo"
 llvm::Expected<std::string> parsePath(llvm::StringRef Path) {
@@ -207,11 +192,11 @@
     const std::string &To = Dir == PathMapping::Direction::ClientToServer
                                 ? Mapping.ServerPath
                                 : Mapping.ClientPath;
-    if (mappingMatches(Uri->body(), From)) {
-      std::string MappedBody = std::move(Uri->body());
-      MappedBody.replace(MappedBody.find(From), From.length(), To);
-      auto MappedUri = URI(Uri->scheme(), Uri->authority(), MappedBody.c_str());
-      return MappedUri.toString();
+    llvm::StringRef Body = Uri->body();
+    if (Body.consume_front(From) && (Body.empty() || Body.front() == '/')) {
+      std::string MappedBody = (To + Body).str();
+      return URI(Uri->scheme(), Uri->authority(), MappedBody.c_str())
+          .toString();
     }
   }
   return llvm::None;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64305.236293.patch
Type: text/x-patch
Size: 1931 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200106/e42cbe3c/attachment.bin>


More information about the cfe-commits mailing list