Skip to content

Commit

Permalink
fix LSP syncing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pommicket committed Sep 10, 2023
1 parent 1b49244 commit e0066fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
25 changes: 14 additions & 11 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,14 +1901,13 @@ void buffer_apply_lsp_text_edits(TextBuffer *buffer, const LSPResponse *response
free(edits);
}

static void buffer_send_lsp_did_change(LSP *lsp, TextBuffer *buffer, BufferPos pos,
u32 nchars_deleted, String32 new_text) {
static void buffer_send_lsp_did_change(LSP *lsp, TextBuffer *buffer, LSPPosition pos,
LSPPosition end, String32 new_text) {
if (!buffer_is_named_file(buffer))
return; // this isn't a named buffer so we can't send a didChange request.
LSPRange range = {0};
range.start = buffer_pos_to_lsp_position(buffer, pos);
BufferPos pos_end = buffer_pos_advance(buffer, pos, nchars_deleted);
range.end = buffer_pos_to_lsp_position(buffer, pos_end);
range.start = pos;
range.end = end;
const char *document = buffer->path;

if (lsp_has_incremental_sync_support(lsp)) {
Expand Down Expand Up @@ -2084,9 +2083,10 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32
// we need to do this *after* making the change to the buffer
// because of how non-incremental syncing works.
LSP *lsp = buffer_lsp(buffer);
if (lsp)
buffer_send_lsp_did_change(lsp, buffer, pos, 0, str_start);

if (lsp) {
LSPPosition lsp_pos = buffer_pos_to_lsp_position(buffer, pos);
buffer_send_lsp_did_change(lsp, buffer, lsp_pos, lsp_pos, str_start);
}
const EditInfo info = {
.pos = pos,
.end = b,
Expand Down Expand Up @@ -2565,6 +2565,7 @@ void buffer_delete_chars_at_pos(TextBuffer *buffer, BufferPos pos, i64 nchars_)
u32 index = pos.index;
Line *line = &buffer->lines[line_idx], *lines_end = &buffer->lines[buffer->nlines];
const BufferPos end_pos = buffer_pos_advance(buffer, pos, nchars);
const LSPPosition end_pos_lsp = buffer_pos_to_lsp_position(buffer, end_pos);

if (nchars + index > line->len) {
// delete rest of line
Expand Down Expand Up @@ -2617,9 +2618,11 @@ void buffer_delete_chars_at_pos(TextBuffer *buffer, BufferPos pos, i64 nchars_)
// we need to do this *after* making the change to the buffer
// because of how non-incremental syncing works.
LSP *lsp = buffer_lsp(buffer);
if (lsp)
buffer_send_lsp_did_change(lsp, buffer, pos, deletion_len, (String32){0});

if (lsp) {
LSPPosition pos_lsp = buffer_pos_to_lsp_position(buffer, pos);
buffer_send_lsp_did_change(lsp, buffer, pos_lsp, end_pos_lsp, (String32){0});
}

buffer_lines_modified(buffer, line_idx, line_idx);
signature_help_retrigger(buffer->ted);

Expand Down
14 changes: 7 additions & 7 deletions lsp-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,6 @@ static void message_writer_write_and_free(LSP *lsp, JSONWriter *o) {
size_t header_size = strlen(header_str);
char *content = &builder.str[max_header_size - header_size];
memcpy(content, header_str, header_size);

#if LSP_SHOW_C2S
const int limit = 1000;
debug_println("%s%.*s%s%s",term_italics(stdout),limit,content,
strlen(content) > (size_t)limit ? "..." : "",
term_clear(stdout));
#endif

if (lsp->log) {
fprintf(lsp->log, "LSP MESSAGE FROM CLIENT TO SERVER\n%s\n\n", content + header_size);
Expand All @@ -356,6 +349,13 @@ static void message_writer_write_and_free(LSP *lsp, JSONWriter *o) {
}

str_builder_free(&builder);

#if LSP_SHOW_C2S
const int limit = 1000;
debug_println("%s%.*s%s%s",term_bold(stdout),limit,content,
strlen(content) > (size_t)limit ? "..." : "",
term_clear(stdout));
#endif
}

static void write_symbol_tag_support(JSONWriter *o) {
Expand Down
1 change: 0 additions & 1 deletion lsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,3 @@ void lsp_write_quit(void);
#define LSP_SHOW_C2S 0

#endif // LSP_INTERNAL

2 changes: 0 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
TODO:
- what's wrong with clangd diagnostics?
FUTURE FEATURES:
- autodetect indentation (tabs vs spaces)
- custom file/build command associations
Expand Down

0 comments on commit e0066fa

Please sign in to comment.