nixd
Loading...
Searching...
No Matches
SourceCode.h
Go to the documentation of this file.
1//===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Various code that examines C++ source code without using heavy AST machinery
10// (and often not even the lexer). To be used sparingly!
11//
12//===----------------------------------------------------------------------===//
13#pragma once
14
15#include "Protocol.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/Support/Error.h"
19#include <optional>
20#include <string>
21
22namespace lspserver {
23
24template <class Type> class Key {
25public:
26 static_assert(!std::is_reference_v<Type>,
27 "Reference arguments to Key<> are not allowed");
28
29 constexpr Key() = default;
30
31 Key(Key const &) = delete;
32 Key &operator=(Key const &) = delete;
33 Key(Key &&) = delete;
34 Key &operator=(Key &&) = delete;
35};
36
37// This context variable controls the behavior of functions in this file
38// that convert between LSP offsets and native clang byte offsets.
39// If not set, defaults to UTF-16 for backwards-compatibility.
41
42// Counts the number of UTF-16 code units needed to represent a string (LSP
43// specifies string lengths in UTF-16 code units).
44// Use of UTF-16 may be overridden by kCurrentOffsetEncoding.
45size_t lspLength(llvm::StringRef Code);
46
47/// Turn a [line, column] pair into an offset in Code.
48///
49/// If P.character exceeds the line length, returns the offset at end-of-line.
50/// (If !AllowColumnsBeyondLineLength, then returns an error instead).
51/// If the line number is out of range, returns an error.
52///
53/// The returned value is in the range [0, Code.size()].
54llvm::Expected<size_t>
55positionToOffset(llvm::StringRef Code, Position P,
57
58/// Turn an offset in Code into a [line, column] pair.
59/// The offset must be in range [0, Code.size()].
60Position offsetToPosition(llvm::StringRef Code, size_t Offset);
61
62// Expand range `A` to also contain `B`.
64
65/// Apply an incremental update to a text document.
66llvm::Error applyChange(std::string &Contents,
68
69/// Collects words from the source code.
70/// Unlike collectIdentifiers:
71/// - also finds text in comments:
72/// - splits text into words
73/// - drops stopwords like "get" and "for"
74llvm::StringSet<> collectWords(llvm::StringRef Content);
75
76} // namespace lspserver
constexpr Key()=default
Key(Key &&)=delete
Key & operator=(Key const &)=delete
Key(Key const &)=delete
Key & operator=(Key &&)=delete
Whether current platform treats paths case insensitively.
Definition Connection.h:11
void unionRanges(Range &A, Range B)
llvm::Error applyChange(std::string &Contents, const TextDocumentContentChangeEvent &Change)
Apply an incremental update to a text document.
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
size_t lspLength(llvm::StringRef Code)
llvm::StringSet collectWords(llvm::StringRef Content)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength=true)
Key< OffsetEncoding > kCurrentOffsetEncoding