[clangd] Cleanup the duplicated getTokenRange.

Summary:
Also lift it to SourceCode.h, so that it can be used in other places
(semantic code highlighting).

Reviewers: kadircet

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D63714

llvm-svn: 364280
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index 6c52ca6..2dcf3d4 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -196,6 +196,17 @@
   return P;
 }
 
+llvm::Optional<Range> getTokenRange(const SourceManager &SM,
+                                    const LangOptions &LangOpts,
+                                    SourceLocation TokLoc) {
+  if (!TokLoc.isValid())
+    return llvm::None;
+  SourceLocation End = Lexer::getLocForEndOfToken(TokLoc, 0, SM, LangOpts);
+  if (!End.isValid())
+    return llvm::None;
+  return halfOpenToRange(SM, CharSourceRange::getCharRange(TokLoc, End));
+}
+
 bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {
   if (!R.getBegin().isValid() || !R.getEnd().isValid())
     return false;