-
Notifications
You must be signed in to change notification settings - Fork 71
refactor: replace PositionMapper with free-function line_starts API #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
9e1922f
b5ff1bc
d583e96
cc82e7a
3ff7b4b
f037257
6037adc
b8e68e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #include <cstdint> | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include <optional> | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include <span> | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string_view> | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #include "compile/compilation.h" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,16 +17,19 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| namespace clice::feature { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| namespace lsp = kota::ipc::lsp; | ||||||||||||||||||||||||||||||||||||||||||||||||
| namespace protocol = kota::ipc::protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| using kota::ipc::lsp::PositionEncoding; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using kota::ipc::lsp::PositionMapper; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using kota::ipc::lsp::parse_position_encoding; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| inline auto to_range(const PositionMapper& converter, LocalSourceRange range) -> protocol::Range { | ||||||||||||||||||||||||||||||||||||||||||||||||
| inline auto to_range(std::string_view content, | ||||||||||||||||||||||||||||||||||||||||||||||||
| std::span<const std::uint32_t> line_starts, | ||||||||||||||||||||||||||||||||||||||||||||||||
| lsp::PositionEncoding encoding, | ||||||||||||||||||||||||||||||||||||||||||||||||
| LocalSourceRange range) -> protocol::Range { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return protocol::Range{ | ||||||||||||||||||||||||||||||||||||||||||||||||
| .start = *converter.to_position(range.begin), | ||||||||||||||||||||||||||||||||||||||||||||||||
| .end = *converter.to_position(range.end), | ||||||||||||||||||||||||||||||||||||||||||||||||
| .start = *lsp::to_position(content, line_starts, encoding, range.begin), | ||||||||||||||||||||||||||||||||||||||||||||||||
| .end = *lsp::to_position(content, line_starts, encoding, range.end), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unchecked optional dereference may cause undefined behavior.
Consider either:
🛡️ Option 1: Add defensive assert inline auto to_range(std::string_view content,
std::span<const std::uint32_t> line_starts,
lsp::PositionEncoding encoding,
LocalSourceRange range) -> protocol::Range {
+ assert(range.end <= content.size() && "range extends beyond content");
+ auto start = lsp::to_position(content, line_starts, encoding, range.begin);
+ auto end = lsp::to_position(content, line_starts, encoding, range.end);
+ assert(start && end && "to_position failed for valid range");
return protocol::Range{
- .start = *lsp::to_position(content, line_starts, encoding, range.begin),
- .end = *lsp::to_position(content, line_starts, encoding, range.end),
+ .start = *start,
+ .end = *end,
};
}🛡️ Option 2: Return optional for caller handling inline auto to_range(std::string_view content,
std::span<const std::uint32_t> line_starts,
lsp::PositionEncoding encoding,
- LocalSourceRange range) -> protocol::Range {
+ LocalSourceRange range) -> std::optional<protocol::Range> {
+ auto start = lsp::to_position(content, line_starts, encoding, range.begin);
+ auto end = lsp::to_position(content, line_starts, encoding, range.end);
+ if (!start || !end)
+ return std::nullopt;
return protocol::Range{
- .start = *lsp::to_position(content, line_starts, encoding, range.begin),
- .end = *lsp::to_position(content, line_starts, encoding, range.end),
+ .start = *start,
+ .end = *end,
};
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -929,13 +929,14 @@ auto inlay_hints(CompilationUnitRef unit, | |||||||||||||||||||||||||||||||
| PositionEncoding encoding) -> std::vector<protocol::InlayHint> { | ||||||||||||||||||||||||||||||||
| auto collected = inlay_hints(unit, target, options); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| PositionMapper converter(unit.interested_content(), encoding); | ||||||||||||||||||||||||||||||||
| auto content = unit.interested_content(); | ||||||||||||||||||||||||||||||||
| auto line_starts = lsp::build_line_starts(content); | ||||||||||||||||||||||||||||||||
| std::vector<protocol::InlayHint> hints; | ||||||||||||||||||||||||||||||||
| hints.reserve(collected.size()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for(const auto& hint: collected) { | ||||||||||||||||||||||||||||||||
| protocol::InlayHint out{ | ||||||||||||||||||||||||||||||||
| .position = *converter.to_position(hint.offset), | ||||||||||||||||||||||||||||||||
| .position = *lsp::to_position(content, line_starts, encoding, hint.offset), | ||||||||||||||||||||||||||||||||
| .label = hint.label, | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
Comment on lines
935
to
939
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unchecked optional dereference could cause undefined behavior.
Consider adding a guard: 🛡️ Proposed defensive fix for(const auto& hint: collected) {
+ auto pos = to_position(unit, encoding, hint.offset);
+ if(!pos) {
+ continue;
+ }
protocol::InlayHint out{
- .position = *to_position(unit, encoding, hint.offset),
+ .position = *pos,
.label = hint.label,
};📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.