Skip to content

Commit

Permalink
In the LSP adapter, properly reference the text to be formatted.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696971926
  • Loading branch information
dplassgit authored and copybara-github committed Nov 15, 2024
1 parent bff2223 commit 64e6d11
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions xls/dslx/fmt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)

Expand Down
7 changes: 5 additions & 2 deletions xls/dslx/fmt/ast_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ inline constexpr int64_t kDslxDefaultTextWidth = 100;
// Auto-formatting entry points.
//
// Performs a reflow-capable formatting of module `m` with standard line width.
// This method is deprecated and will be removed soon. Use the AutoFmt method
// below instead.
std::string LegacyAutoFmt(const Module& m, const Comments& comments,
int64_t text_width = kDslxDefaultTextWidth);

Expand All @@ -62,9 +64,10 @@ absl::StatusOr<std::string> AutoFmt(const Module& m, Comments& comments,

// Performs a reflow-capable formatting of module `m` with standard line width,
// for the actual `content` but with the ability to disable formatting for
// specific ranges of text. This is only intended to be used for testing.
// specific ranges of text.
absl::StatusOr<std::string> AutoFmt(const Module& m, Comments& comments,
std::string contents, int64_t text_width);
std::string contents,
int64_t text_width = kDslxDefaultTextWidth);

// If we fail the postcondition we return back the data we used to detect that
// the postcondition was violated.
Expand Down
8 changes: 5 additions & 3 deletions xls/dslx/fmt/format_disabler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "xls/common/file/filesystem.h"
Expand Down Expand Up @@ -186,12 +187,13 @@ absl::StatusOr<std::string> FormatDisabler::GetTextInSpan(const Span &span) {
int64_t start_line = span.start().lineno();
int64_t end_line = span.limit().lineno();
if (start_line < 0 || start_line > lines_.size()) {
return absl::InvalidArgumentError(
absl::StrCat("Start line out of bounds: ", start_line));
return absl::InvalidArgumentError(absl::StrFormat(
"Start line %d out of bounds: max %d", start_line, lines_.size()));
}
if (end_line < 0 || end_line > lines_.size() || end_line < start_line) {
return absl::InvalidArgumentError(
absl::StrCat("End line out of bounds: ", end_line));
absl::StrFormat("End line %d out of bounds: start %d, max %d", end_line,
start_line, lines_.size()));
}

int64_t end_column = span.limit().colno();
Expand Down
9 changes: 8 additions & 1 deletion xls/dslx/lsp/language_server_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,16 @@ absl::StatusOr<std::vector<verible::lsp::TextEdit>>
LanguageServerAdapter::FormatDocument(std::string_view uri) const {
using ResultT = std::vector<verible::lsp::TextEdit>;
if (ParseData* parsed = FindParsedForUri(uri); parsed && parsed->ok()) {
if (!vfs_contents_.contains(uri)) {
// TODO: davidplass - We should probably format the contents of the file
// anyway, even if we haven't received the full text from the LSP
// server. The user won't be able to use dslx-fmt::off, but that's OK.
return ResultT{};
}
const Module& module = parsed->module();
const std::string& dslx_code = vfs_contents_.at(uri);
XLS_ASSIGN_OR_RETURN(std::string new_contents,
AutoFmt(module, parsed->comments()));
AutoFmt(module, parsed->comments(), dslx_code));
return ResultT{
verible::lsp::TextEdit{.range = ConvertSpanToLspRange(module.span()),
.newText = new_contents}};
Expand Down
28 changes: 28 additions & 0 deletions xls/dslx/lsp/language_server_adapter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,34 @@ fn messy() {
)");
}

TEST(LanguageServerAdapterTest, DisableFormatting) {
LanguageServerAdapter adapter(kDefaultDslxStdlibPath, /*dslx_paths=*/{"."});
constexpr std::string_view kUri = "memfile://test.x";
constexpr std::string_view kInput = R"(// Top of module comment.
// dslx-fmt::off
fn messy(){()// retval comment
}
// dslx-fmt::on)";
const auto kInputRange =
verible::lsp::Range{.start = verible::lsp::Position{0, 0},
.end = verible::lsp::Position{5, 15}};

// Notify the adapter of the file contents.
XLS_ASSERT_OK(adapter.Update(kUri, kInput));

// Act
XLS_ASSERT_OK_AND_ASSIGN(std::vector<verible::lsp::TextEdit> edits,
adapter.FormatDocument(kUri));

// Assert
ASSERT_EQ(edits.size(), 1);
const verible::lsp::TextEdit& edit = edits.at(0);
EXPECT_TRUE(edit.range == kInputRange) << DebugString(edit.range);
// Text should be unchanged, because we disabled formatting.
EXPECT_EQ(edit.newText, kInput);
}

TEST(LanguageServerAdapterTest, InlayHintForLetStatement) {
LanguageServerAdapter adapter(kDefaultDslxStdlibPath, /*dslx_paths=*/{"."});
constexpr std::string_view kUri = "memfile://test.x";
Expand Down

0 comments on commit 64e6d11

Please sign in to comment.