CIRCT 22.0.0git
Loading...
Searching...
No Matches
VerilogTextFile.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// VerilogTextFile.cpp
10//
11// This file implements the VerilogTextFile class, a lightweight wrapper around
12// VerilogDocument that represents the open text buffer for a single source file
13// managed by the CIRCT Verilog LSP server.
14//
15// Responsibilities:
16// * Manage the current text contents and version of an open Verilog file.
17// * Rebuild the associated VerilogDocument whenever the file is opened or
18// updated via LSP “didOpen” or “didChange” notifications.
19// * Apply incremental text changes as specified by the LSP protocol.
20// * Forward symbol-definition and reference queries to the underlying
21// VerilogDocument.
22//
23// The class acts as the LSP-facing façade for each active text document,
24// maintaining an editable in-memory copy of its contents while keeping the
25// corresponding Slang-based VerilogDocument synchronized for semantic analysis.
26//
27//===----------------------------------------------------------------------===//
28
29#include "VerilogTextFile.h"
30#include "../Utils/LSPUtils.h"
31#include "VerilogDocument.h"
32
33using namespace circt::lsp;
34using namespace llvm;
35using namespace llvm::lsp;
36
38 VerilogServerContext &context, const llvm::lsp::URIForFile &uri,
39 StringRef fileContents, int64_t version,
40 std::vector<llvm::lsp::Diagnostic> &diagnostics)
41 : context(context), contents(fileContents.str()) {
42 initialize(uri, version, diagnostics);
43}
44
46 const llvm::lsp::URIForFile &uri, int64_t newVersion,
47 ArrayRef<llvm::lsp::TextDocumentContentChangeEvent> changes,
48 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
49 if (failed(llvm::lsp::TextDocumentContentChangeEvent::applyTo(changes,
50 contents))) {
51 circt::lsp::Logger::error(Twine("Failed to update contents of ") +
52 uri.file());
53 return failure();
54 }
55
56 // If the file contents were properly changed, reinitialize the text file.
57 initialize(uri, newVersion, diagnostics);
58 return success();
59}
60
62 const llvm::lsp::URIForFile &uri, int64_t newVersion,
63 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
64 version = newVersion;
65 document =
66 std::make_unique<VerilogDocument>(context, uri, contents, diagnostics);
67}
68
70 const llvm::lsp::URIForFile &uri, llvm::lsp::Position defPos,
71 std::vector<llvm::lsp::Location> &locations) {
72 document->getLocationsOf(uri, defPos, locations);
73}
74
76 const llvm::lsp::URIForFile &uri, llvm::lsp::Position pos,
77 std::vector<llvm::lsp::Location> &references) {
78 document->findReferencesOf(uri, pos, references);
79}
void initialize(const llvm::lsp::URIForFile &uri, int64_t newVersion, std::vector< llvm::lsp::Diagnostic > &diagnostics)
Initialize the text file from the given file contents.
VerilogServerContext & context
void getLocationsOf(const llvm::lsp::URIForFile &uri, llvm::lsp::Position defPos, std::vector< llvm::lsp::Location > &locations)
void findReferencesOf(const llvm::lsp::URIForFile &uri, llvm::lsp::Position pos, std::vector< llvm::lsp::Location > &references)
std::string contents
The full string contents of the file.
int64_t version
The version of this file.
llvm::LogicalResult update(const llvm::lsp::URIForFile &uri, int64_t newVersion, llvm::ArrayRef< llvm::lsp::TextDocumentContentChangeEvent > changes, std::vector< llvm::lsp::Diagnostic > &diagnostics)
Update the file to the new version using the provided set of content changes.
VerilogTextFile(VerilogServerContext &globalContext, const llvm::lsp::URIForFile &uri, llvm::StringRef fileContents, int64_t version, std::vector< llvm::lsp::Diagnostic > &diagnostics)
std::unique_ptr< circt::lsp::VerilogDocument > document
The chunks of this file.
void error(Twine message)
Definition LSPUtils.cpp:16