working for starting screen

This commit is contained in:
2026-06-02 19:06:51 +02:00
parent 2738bc8042
commit cec92cacd1
6 changed files with 329 additions and 254 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
#define SPACE "\x20" #define SPACE "\x20"
/* Uncomment to see debug logs on stderr */ /* Uncomment to see debug logs on stderr */
// #define APP_DEBUG #define APP_DEBUG
#define COMPLETION_MAX_ITEMS 16 #define COMPLETION_MAX_ITEMS 16
#define COMPLETION_MAX_WIDTH 40 #define COMPLETION_MAX_WIDTH 40
+4 -3
View File
@@ -48,14 +48,15 @@ int main(int argc, char *argv[]) {
active->buffer_id = bufferCreate(splash_screen, READ_ONLY); active->buffer_id = bufferCreate(splash_screen, READ_ONLY);
if (argc >= 2) { if (argc >= 2) {
if (E.constantes.LSP) {
}
active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE); active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE);
buf = &E.buffers[active->buffer_id]; buf = &E.buffers[active->buffer_id];
appDebug("peoject root : %s\n", dirname(buf->fullname)); appDebug("peoject root : %s\n", dirname(buf->fullname));
if (E.constantes.LSP) {
lspDidOpen(E.lsp_client, buf);
}
} }
bFree(splash_screen); bFree(splash_screen);
+7 -1
View File
@@ -63,6 +63,7 @@ struct buffer_t* bufferFindById(int buffer_id)
*/ */
int bufferCreate(const char* path, enum bufferStatus_e state) int bufferCreate(const char* path, enum bufferStatus_e state)
{ {
appDebug("Creating new buffer");
char *filename = basename((char *) path); char *filename = basename((char *) path);
// Check if file is already open // Check if file is already open
const int existing_id = bufferFindByFilename(path); const int existing_id = bufferFindByFilename(path);
@@ -89,13 +90,18 @@ int bufferCreate(const char* path, enum bufferStatus_e state)
new_buf->x = 0; new_buf->x = 0;
new_buf->y = 0; new_buf->y = 0;
new_buf->dirty = 0; // New file starts clean new_buf->dirty = 0; // New file starts clean
new_buf->b_lsp_open = 0;
// Load file content using existing editorOpen // Load file content using existing editorOpen
editorOpen(new_buf); editorOpen(new_buf);
E.number_of_buffer++; E.number_of_buffer++;
if (new_buf->filename[strlen(new_buf->filename) - 1] == 'c') if (new_buf->filename[strlen(new_buf->filename) - 1] == 'c')
{ {
lspStart(E.lsp_client, new_buf->path); if (E.lsp_client->state == LSP_SHUTDOWN)
lspStart(E.lsp_client, dirname(new_buf->path));
while (E.lsp_client->state != LSP_READY)
;
lspDidOpen(E.lsp_client, new_buf);
} }
editorSetStatusMessage("Opened: %s (buffer %d)", filename, new_buf->buffer_id); editorSetStatusMessage("Opened: %s (buffer %d)", filename, new_buf->buffer_id);
+34 -17
View File
@@ -396,6 +396,7 @@ static char* buffer_to_text(struct buffer_t* buf)
void lspDidOpen(LspClient* lsp, struct buffer_t* buf) void lspDidOpen(LspClient* lsp, struct buffer_t* buf)
{ {
appDebug("[LSP] opening file");
if (lsp->state != LSP_READY || buf->b_lsp_open) return; if (lsp->state != LSP_READY || buf->b_lsp_open) return;
@@ -546,48 +547,64 @@ void lspRequestDefinition(LspClient* lsp, struct buffer_t* buf, int line, int co
bFree(msg); bFree(msg);
} }
void lspShutdown(LspClient* lsp) void lspShutdown(LspClient *lsp)
{ {
if (!lsp || lsp->state == LSP_SHUTDOWN) return; if (!lsp || lsp->state == LSP_SHUTDOWN) return;
lsp->state = LSP_SHUTDOWN; lsp->state = LSP_SHUTDOWN;
// 1. Send shutdown request (clangd expects this before exit) // 1. Send didClose for all open buffers
cJSON* req = cJSON_CreateObject(); for (int i = 0; i < E.number_of_buffer; i++) {
if (E.buffers[i].b_lsp_open)
lspDidClose(lsp, &E.buffers[i]);
}
// 2. Send shutdown request (clangd expects this before exit)
cJSON *req = cJSON_CreateObject();
cJSON_AddStringToObject(req, "jsonrpc", "2.0"); cJSON_AddStringToObject(req, "jsonrpc", "2.0");
cJSON_AddNumberToObject(req, "id", lsp->next_id++); cJSON_AddNumberToObject(req, "id", lsp->next_id++);
cJSON_AddStringToObject(req, "method", "shutdown"); cJSON_AddStringToObject(req, "method", "shutdown");
cJSON_AddNullToObject(req, "params"); cJSON_AddNullToObject (req, "params");
char* msg = cJSON_PrintUnformatted(req); char *msg = cJSON_PrintUnformatted(req);
lsp_send(lsp->write_fd, msg); lsp_send(lsp->write_fd, msg);
bFree(msg); bFree(msg);
cJSON_Delete(req); cJSON_Delete(req);
// 2. Wait briefly for the shutdown response // 3. Wait briefly for the shutdown response (2s timeout)
// (don't block forever — clangd has 2s to reply) struct timeval tv = { .tv_sec = 2, .tv_usec = 0 };
struct timeval tv = {.tv_sec = 2, .tv_usec = 0};
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(lsp->read_fd, &fds); FD_SET(lsp->read_fd, &fds);
if (select(lsp->read_fd + 1, &fds, NULL, NULL, &tv) > 0) if (select(lsp->read_fd + 1, &fds, NULL, NULL, &tv) > 0) {
{ char *resp = lsp_recv(lsp->read_fd);
char* resp = lsp_recv(lsp->read_fd);
bFree(resp); bFree(resp);
} }
// 3. Send exit notification // 4. Send exit notification
lsp_send(lsp->write_fd, lsp_send(lsp->write_fd,
"{\"jsonrpc\":\"2.0\",\"method\":\"exit\",\"params\":null}"); "{\"jsonrpc\":\"2.0\",\"method\":\"exit\",\"params\":null}");
// 4. Close pipes — this signals the reader thread to stop // 5. Close write pipe first — reader thread will get EOF and exit
close(lsp->write_fd); close(lsp->write_fd);
close(lsp->read_fd); lsp->write_fd = -1;
// 5. Wait for reader thread to finish // 6. Wake the main loop so it doesn't stay blocked in select()
write(lsp->wake_pipe[1], "q", 1);
// 7. Wait for reader thread to finish
pthread_join(lsp->reader_thread, NULL); pthread_join(lsp->reader_thread, NULL);
pthread_mutex_destroy(&lsp->lock);
// 6. Reap the clangd process // 8. Close remaining fds
close(lsp->read_fd);
lsp->read_fd = -1;
close(lsp->wake_pipe[0]);
close(lsp->wake_pipe[1]);
// 9. Destroy synchronization primitives
pthread_mutex_destroy(&lsp->lock);
pthread_cond_destroy (&lsp->ready_cond);
// 10. Reap the clangd process
waitpid(lsp->pid, NULL, 0); waitpid(lsp->pid, NULL, 0);
bFree(lsp); bFree(lsp);
+7 -8
View File
@@ -209,8 +209,10 @@ int executeKeyBind(char *key_sequence) {
void editorProcessKeypress() { void editorProcessKeypress() {
int c = editorReadKey(); int c = editorReadKey();
char key_sequence[8]; char key_sequence[8];
EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buf = bufferFindById(active->buffer_id);
if (E.constantes.LSP) { if (E.constantes.LSP && buf->b_lsp_open) {
if (c == LSP_WAKE_KEY) if (c == LSP_WAKE_KEY)
return; return;
if (E.lsp_client && E.lsp_completion.visible) { if (E.lsp_client && E.lsp_completion.visible) {
@@ -227,8 +229,7 @@ void editorProcessKeypress() {
if (c == '\r') { if (c == '\r') {
CompletionItem *item = CompletionItem *item =
&E.lsp_completion.items[E.lsp_completion.selected]; &E.lsp_completion.items[E.lsp_completion.selected];
EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buf = bufferFindById(active->buffer_id);
// Find how many chars the user already typed by looking at the // Find how many chars the user already typed by looking at the
// current word (chars before cursor on the same line) // current word (chars before cursor on the same line)
@@ -267,16 +268,14 @@ void editorProcessKeypress() {
int seq_len = utf8Encode(c, key_sequence); int seq_len = utf8Encode(c, key_sequence);
appDebug("key seq : %s\n", key_sequence); appDebug("key seq : %s\n", key_sequence);
bufferInsertBytes(key_sequence, seq_len); bufferInsertBytes(key_sequence, seq_len);
if (E.constantes.LSP && is_word_char(key_sequence)) { if (buf->b_lsp_open && is_word_char(key_sequence)) {
EditorPane *active = splitScreenGetActivePane();
struct buffer_t *buffer = bufferFindById(active->buffer_id);
if (E.lsp_client && E.lsp_client->state == LSP_READY) { if (E.lsp_client && E.lsp_client->state == LSP_READY) {
lspDidChange(E.lsp_client, buffer); lspDidChange(E.lsp_client, buf);
E.lsp_client->completion_just_arrived = 0; // consume the flag E.lsp_client->completion_just_arrived = 0; // consume the flag
} }
buffer->b_has_changed = 0; buf->b_has_changed = 0;
editorAutoComplete(lisp_null(), &E.ctx_error, E.ctx); editorAutoComplete(lisp_null(), &E.ctx_error, E.ctx);
} }
E.quit_times_buffer = E.constantes.QUIT_TIMES; E.quit_times_buffer = E.constantes.QUIT_TIMES;
+101 -49
View File
@@ -10,9 +10,12 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "../include/buffer.h"
#include "../include/split_screen.h"
#include "include/utf8.h" #include "include/utf8.h"
void die(const char *s) { void die(const char* s)
{
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
lisp_shutdown(E.ctx); lisp_shutdown(E.ctx);
@@ -20,14 +23,18 @@ void die(const char *s) {
exit(1); exit(1);
} }
void disableRawMode() { void disableRawMode()
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1) { {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
{
die("tcsetattr"); die("tcsetattr");
} }
} }
void enableRawMode() { void enableRawMode()
if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) { {
if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1)
{
die("tcgetattr"); die("tcgetattr");
} }
@@ -39,24 +46,34 @@ void enableRawMode() {
raw.c_cc[VMIN] = 0; raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1; raw.c_cc[VTIME] = 1;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
{
die("tcgetattr"); die("tcgetattr");
} }
} }
#include <ctype.h> /* isprint */ #include <ctype.h> /* isprint */
char *keyToString(int key) { char* keyToString(int key)
{
static char key_str[32]; static char key_str[32];
if (key == '\r') { if (key == '\r')
{
strcpy(key_str, "ENTER"); strcpy(key_str, "ENTER");
} else if (key == 0x09) { }
else if (key == 0x09)
{
strcpy(key_str, "TAB"); strcpy(key_str, "TAB");
} else if (key >= 1 && key <= 26) { }
else if (key >= 1 && key <= 26)
{
snprintf(key_str, sizeof(key_str), "CTRL-%c", 'a' + key - 1); snprintf(key_str, sizeof(key_str), "CTRL-%c", 'a' + key - 1);
} else { }
switch (key) { else
{
switch (key)
{
case ARROW_UP: case ARROW_UP:
strcpy(key_str, "ARROW-UP"); strcpy(key_str, "ARROW-UP");
break; break;
@@ -91,14 +108,19 @@ char *keyToString(int key) {
strcpy(key_str, "ESCAPE"); strcpy(key_str, "ESCAPE");
break; break;
default: default:
if (key > 127) { if (key > 127)
{
/* UTF-8 code point — re-encode into the buffer */ /* UTF-8 code point — re-encode into the buffer */
char buf[5] = {0}; char buf[5] = {0};
int n = utf8Encode((uint32_t)key, buf); int n = utf8Encode((uint32_t)key, buf);
snprintf(key_str, sizeof(key_str), "%.*s", n, buf); snprintf(key_str, sizeof(key_str), "%.*s", n, buf);
} else if (isprint(key)) { }
else if (isprint(key))
{
snprintf(key_str, sizeof(key_str), "%c", key); snprintf(key_str, sizeof(key_str), "%c", key);
} else { }
else
{
snprintf(key_str, sizeof(key_str), "KEY-%d", key); snprintf(key_str, sizeof(key_str), "KEY-%d", key);
} }
} }
@@ -106,46 +128,57 @@ char *keyToString(int key) {
return key_str; return key_str;
} }
int editorReadKey() { int editorReadKey()
{
char c; char c;
int nread; int nread;
while (1) { while (1)
{
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); FD_SET(STDIN_FILENO, &fds);
// Also watch the LSP wake pipe if available
int max_fd = STDIN_FILENO; int max_fd = STDIN_FILENO;
if (E.lsp_client && E.lsp_client->wake_pipe[0] > 0) {
// Only watch wake pipe if LSP is ready AND active buffer is open
EditorPane* active = splitScreenGetActivePane();
struct buffer_t* buf = active ? bufferFindById(active->buffer_id) : NULL;
int lsp_active = E.lsp_client
&& E.lsp_client->wake_pipe[0] > 0
&& E.lsp_client->state == LSP_READY
&& buf
&& buf->b_lsp_open; // ← only if this buffer is tracked
if (lsp_active)
{
FD_SET(E.lsp_client->wake_pipe[0], &fds); FD_SET(E.lsp_client->wake_pipe[0], &fds);
if (E.lsp_client->wake_pipe[0] > max_fd) if (E.lsp_client->wake_pipe[0] > max_fd)
max_fd = E.lsp_client->wake_pipe[0]; max_fd = E.lsp_client->wake_pipe[0];
} }
int ready = select(max_fd + 1, &fds, NULL, NULL, NULL); int ready = select(max_fd + 1, &fds, NULL, NULL, NULL);
if (ready <= 0) if (ready <= 0) continue;
continue;
// LSP event arrived — drain pipe and trigger a redraw if (lsp_active && FD_ISSET(E.lsp_client->wake_pipe[0], &fds))
// by returning a special no-op key {
if (E.lsp_client && FD_ISSET(E.lsp_client->wake_pipe[0], &fds)) {
char tmp[16]; char tmp[16];
read(E.lsp_client->wake_pipe[0], tmp, sizeof(tmp)); read(E.lsp_client->wake_pipe[0], tmp, sizeof(tmp));
return LSP_WAKE_KEY; // ← special value, see below return LSP_WAKE_KEY;
} }
// Normal keypress if (FD_ISSET(STDIN_FILENO, &fds))
if (FD_ISSET(STDIN_FILENO, &fds)) { {
nread = read(STDIN_FILENO, &c, 1); nread = read(STDIN_FILENO, &c, 1);
if (nread == 1) if (nread == 1) break;
break;
} }
} }
appDebug("f : %hhu %ld\r\n", c, 0x200); appDebug("f : %hhu %ld\r\n", c, 0x200);
if (c == '\x1b') { if (c == '\x1b')
{
char seq[6]; char seq[6];
/* try to read escape sequence */ /* try to read escape sequence */
if (read(STDIN_FILENO, &seq[0], 1) != 1) if (read(STDIN_FILENO, &seq[0], 1) != 1)
@@ -154,12 +187,16 @@ int editorReadKey() {
return '\x1b'; return '\x1b';
appDebug("f2 : %s\r\n", seq); appDebug("f2 : %s\r\n", seq);
if (seq[0] == '[') { if (seq[0] == '[')
if (seq[1] >= '0' && seq[1] <= '9') { {
if (seq[1] >= '0' && seq[1] <= '9')
{
if (read(STDIN_FILENO, &seq[2], 1) != 1) if (read(STDIN_FILENO, &seq[2], 1) != 1)
return '\x1b'; return '\x1b';
if (seq[2] == '~') { if (seq[2] == '~')
switch (seq[1]) { {
switch (seq[1])
{
case '1': case '1':
return BEG_LINE; return BEG_LINE;
case '3': case '3':
@@ -176,8 +213,11 @@ int editorReadKey() {
return END_LINE; return END_LINE;
} }
} }
} else { }
switch (seq[1]) { else
{
switch (seq[1])
{
case 'A': case 'A':
return ARROW_UP; return ARROW_UP;
case 'B': case 'B':
@@ -198,7 +238,8 @@ int editorReadKey() {
/* multi-byte UTF-8: read remaining bytes */ /* multi-byte UTF-8: read remaining bytes */
int seqlen = utf8Seqlen((unsigned char)c); int seqlen = utf8Seqlen((unsigned char)c);
if (seqlen > 1) { if (seqlen > 1)
{
/* pack into a pseudo-codepoint just to pass bytes through; /* pack into a pseudo-codepoint just to pass bytes through;
we handle encoding/decoding at the row level */ we handle encoding/decoding at the row level */
char buf[4] = {c, 0, 0, 0}; char buf[4] = {c, 0, 0, 0};
@@ -206,7 +247,7 @@ int editorReadKey() {
if (read(STDIN_FILENO, &buf[i], 1) != 1) if (read(STDIN_FILENO, &buf[i], 1) != 1)
break; break;
/* decode and return as uint32, but we need int — use high range */ /* decode and return as uint32, but we need int — use high range */
const char *p = buf; const char* p = buf;
uint32_t cp = utf8Decode(&p); uint32_t cp = utf8Decode(&p);
return (int)cp; /* caller re-encodes when inserting */ return (int)cp; /* caller re-encodes when inserting */
} }
@@ -214,40 +255,50 @@ int editorReadKey() {
return (unsigned char)c; return (unsigned char)c;
} }
int getCursorPosition(int *rows, int *cols) { int getCursorPosition(int* rows, int* cols)
{
char buf[32]; char buf[32];
unsigned int i = 0; unsigned int i = 0;
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) { if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4)
{
return -1; return -1;
} }
while (i < sizeof(buf) - 1) { while (i < sizeof(buf) - 1)
if (read(STDIN_FILENO, &buf[i], 1) != 1) { {
if (read(STDIN_FILENO, &buf[i], 1) != 1)
{
break; break;
} }
if (buf[i] == 'R') { if (buf[i] == 'R')
{
break; break;
} }
++i; ++i;
} }
buf[i] = '\0'; buf[i] = '\0';
if (buf[0] != '\x1b' || buf[1] != '[') { if (buf[0] != '\x1b' || buf[1] != '[')
{
return -1; return -1;
} }
if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) { if (sscanf(&buf[2], "%d;%d", rows, cols) != 2)
{
return -1; return -1;
} }
return 0; return 0;
} }
int getWindowSize(int *rows, int *cols) { int getWindowSize(int* rows, int* cols)
{
struct winsize ws; struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) { {
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12)
{
return -1; return -1;
} }
return getCursorPosition(rows, cols); return getCursorPosition(rows, cols);
@@ -257,7 +308,8 @@ int getWindowSize(int *rows, int *cols) {
return 0; return 0;
} }
void appDebug(const char *fmt, ...) { void appDebug(const char* fmt, ...)
{
#ifdef APP_DEBUG #ifdef APP_DEBUG
va_list ap; va_list ap;
char message[1024]; char message[1024];