diff --git a/config/init.lisp b/config/init.lisp index f593733..640fc9b 100644 --- a/config/init.lisp +++ b/config/init.lisp @@ -90,4 +90,5 @@ (map-key "CTRL-k" editor-cut-end-line "no-prefix") (map-key "a" editor-move-cursor-beg-buffer "user") (map-key "z" editor-move-cursor-end-buffer "user") -;; (map-key "x" editor-auto-complete "user") +(map-key "CTRL-h" editor-auto-complete "no-prefix") +(map-key "C-x" lsp-complete "user") diff --git a/include/buffer.h b/include/buffer.h index 818a80a..50fd379 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -13,7 +13,7 @@ * @param filename Path to the file * @return Buffer ID on success, -1 on failure */ -int bufferCreate(const char *filename, enum bufferStatus_e state); +int bufferCreate(const char *path, enum bufferStatus_e state); /** * @brief Switches to a specific buffer by ID diff --git a/include/builtins.h b/include/builtins.h index 5dc9d7d..e529b4c 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -54,7 +54,8 @@ Lisp editorMoveEndBuffer(Lisp args, LispError *e, LispContext ctx); // Other Lisp editorAutoComplete(Lisp args, LispError *e, LispContext ctx); - +// Lisp lspComplete(Lisp args, LispError *e, LispContext ctx); +Lisp lspDefinition(Lisp args, LispError *e, LispContext ctx); void free_structs(void); diff --git a/include/cJSON.h b/include/cJSON.h new file mode 100644 index 0000000..cab5feb --- /dev/null +++ b/include/cJSON.h @@ -0,0 +1,306 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif + +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does + +*/ + +#define CJSON_CDECL __cdecl +#define CJSON_STDCALL __stdcall + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type CJSON_STDCALL +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#endif +#else /* !__WINDOWS__ */ +#define CJSON_CDECL +#define CJSON_STDCALL + +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 7 +#define CJSON_VERSION_PATCH 19 + +#include + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON +{ + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks +{ + /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void (CJSON_CDECL *free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* Limits the length of circular references can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_CIRCULAR_LIMIT +#define CJSON_CIRCULAR_LIMIT 10000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char*) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* Check item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* Create a string where valuestring references a string so + * it will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); +/* Create an object/array that only references it's elements so + * they will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); + +/* These utilities create an Array of count items. + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Helper functions for creating and adding items to an object at the same time. + * They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + cJSON_Invalid\ +) + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/completion.h b/include/completion.h new file mode 100644 index 0000000..afce595 --- /dev/null +++ b/include/completion.h @@ -0,0 +1,29 @@ +// +// Created by Giorgio on 25/05/2026. +// + +#ifndef BELUGA_COMPLETION_H +#define BELUGA_COMPLETION_H +#include "data.h" + + + +void createContextBuffer(const int x, const int y, const char * text); + +int lspStart(LspClient *lsp, const char *project_root); +void lspShutdown(LspClient *lsp); + +// Document sync — call these from your buffer open/save/edit hooks +void lspDidOpen(LspClient *lsp, struct buffer_t *buf); +void lspDidChange(LspClient *lsp, struct buffer_t *buf); +void lspDidClose(LspClient *lsp, struct buffer_t *buf); + +// Requests +void lspRequestCompletion(LspClient *lsp, struct buffer_t *buf, + int line, int col, + int screen_x, int screen_y); +void lspRequestHover(LspClient *lsp, struct buffer_t *buf, int line, int col); +void lspRequestDefinition(LspClient *lsp, struct buffer_t *buf, int line, int col); + + +#endif //BELUGA_COMPLETION_H diff --git a/include/data.h b/include/data.h index 32dd6a0..ca1a529 100644 --- a/include/data.h +++ b/include/data.h @@ -5,8 +5,12 @@ #include #include +#include "define.h" + #include "lisp.h" +typedef struct lsp_client_t LspClient; + /** * \struct row_t * \brief Store one editor row @@ -94,15 +98,98 @@ struct keyBind_t { Lisp command; }; +// In data.h — add these + +#include + +#define LSP_MAX_PENDING 64 + +typedef enum { + LSP_NOT_STARTED = 0, + LSP_INITIALIZING, + LSP_READY, + LSP_SHUTDOWN, +} LspState; + +typedef struct { + int id; + void (*callback)(struct lsp_client_t *lsp, const char *json); +} LspPending; + +typedef struct lsp_client_t { + // ── Process ─────────────────────────────────────────────────────────────── + pid_t pid; + int write_fd; + int read_fd; + int completion_just_arrived; + int completion_requested; + + + + int wake_pipe[2]; // [0] = read end (main loop), [1] = write end (reader thread) + + + // ── State ───────────────────────────────────────────────────────────────── + LspState state; + int next_id; + + // ── Pending requests ────────────────────────────────────────────────────── + LspPending pending[LSP_MAX_PENDING]; + int pending_count; + + // ── Threading ───────────────────────────────────────────────────────────── + pthread_t reader_thread; + pthread_mutex_t lock; + pthread_cond_t ready_cond; // signaled when state → LSP_READY + + // ── Completion context ──────────────────────────────────────────────────── + int completion_cursor_x; // screen position when + int completion_cursor_y; // completion was requested +} LspClient; + +typedef struct { + char label[128]; // display text e.g. "printf" + char detail[64]; // type/sig hint e.g. "int (const char *, ...)" + int kind; // LSP CompletionItemKind (1=Text,2=Method,3=Function…) +} CompletionItem; + +typedef struct { + CompletionItem items[COMPLETION_MAX_ITEMS]; + int count; + int selected; // currently highlighted row + int visible; // is the popup shown? + int origin_x; // screen col where popup appears + int origin_y; // screen row where popup appears +} CompletionPopup; + +typedef enum { DIAG_ERROR = 1, DIAG_WARNING, DIAG_HINT } DiagSeverity; + +typedef struct { + int buffer_id; + int line; // 0-based + int col_start; // 0-based + int col_end; + DiagSeverity severity; + char message[256]; +} Diagnostic; + +typedef struct { + Diagnostic entries[DIAG_MAX]; + int count; +} DiagnosticList; + enum buffer_type { FILE_BUFF, TERMINAL_BUFF }; struct buffer_t { enum buffer_type type; int buffer_id; + int b_lsp_open; int x, y; /**< Position in the file */ row_t *row; int numrows; + int b_has_changed; char *filename; + char *path; enum bufferStatus_e state; int dirty; /**< Has this buffer been modified since last save */ }; @@ -122,6 +209,10 @@ struct editorConfig { ContextBuffer* context_buffers; + LspClient *lsp_client; + CompletionPopup lsp_completion; + DiagnosticList lsp_diagnostics; + int dirty; char *status_msg; diff --git a/include/define.h b/include/define.h index e5aeab3..9dc7d6c 100644 --- a/include/define.h +++ b/include/define.h @@ -14,6 +14,13 @@ #define SPACE "\x20" #define APP_DEBUG +#define COMPLETION_MAX_ITEMS 16 +#define COMPLETION_MAX_WIDTH 40 +#define COMPLETION_POPUP_H 8 // max visible rows +#define DIAG_MAX 128 + +#define GUTTER_WIDTH 2 + enum editorKey_e { BACKSPACE = 127, ARROW_LEFT = 1000, diff --git a/include/init.h b/include/init.h index a2186e5..858e132 100644 --- a/include/init.h +++ b/include/init.h @@ -1,10 +1,6 @@ #ifndef INIT_H_ #define INIT_H_ -#include "builtins.h" -#include "data.h" -#include "terminal.h" -#include /** * \fn void initEditor() diff --git a/include/lsp_ui.h b/include/lsp_ui.h new file mode 100644 index 0000000..3e48ee8 --- /dev/null +++ b/include/lsp_ui.h @@ -0,0 +1,35 @@ +// +// Created by Giorgio on 27/05/2026. +// + +#ifndef BELUGA_LSP_UI_H +#define BELUGA_LSP_UI_H + + +#include "data.h" +#include "buffer.h" // for abuf + + +// ── Public API ──────────────────────────────────────────────────────────────── + +// Call from editorRefreshScreen() — appends popup/underlines to abuf +void lspUiDrawCompletion(struct abuf *ab, CompletionPopup *popup); +void lspUiDrawDiagnostics(struct abuf *ab, DiagnosticList *diags, + EditorPane *pane, struct buffer_t *buf); + +// Input handling — returns 1 if the key was consumed by the popup +int lspUiHandleKey(CompletionPopup *popup, int key); + +// Parse clangd JSON into our structs +void lspParseCompletion(const char *json, CompletionPopup *popup, + int screen_x, int screen_y); +void lspParseDiagnostics(const char *json, DiagnosticList *diags, int buffer_id); + +void lspUiDrawGutter(struct abuf *ab, DiagnosticList *diags, + int buf_id, int line); +const char *lspUiDiagnosticAtCursor(DiagnosticList *diags, + int buffer_id, int cursor_line); + + +#endif + diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..2721f72 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,16 @@ +// +// Created by Giorgio on 28/05/2026. +// + +#ifndef BELUGA_UTILS_H +#define BELUGA_UTILS_H +#include + +extern int beluga_alloc_counter; + + +void * bAlloc(size_t size); +void * bRealloc(void * ptr, size_t size); +void * bFree(void * ptr); + +#endif //BELUGA_UTILS_H diff --git a/main.c b/main.c index 954cce3..f5080b5 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,8 @@ #define _BSD_SOURCE #define _GNU_SOURCE +#include "include/utils.h" + #include "include/buffer.h" #include "include/split_screen.h" #include @@ -19,18 +21,36 @@ #include "include/input.h" #include "include/editor_op.h" #include "include/terminal.h" +#include "include/completion.h" +#include + +#include "include/utils.h" struct editorConfig E; int main(int argc, char *argv[]) { - char * splash_screen = (char *) calloc(256, sizeof(char)); + char * splash_screen = bAlloc(sizeof(char) * 512); + + signal(SIGPIPE, SIG_IGN); // don't die on broken pipe, just get EPIPE from write() enableRawMode(); initEditor(); if (argc >= 2) { EditorPane *active = splitScreenGetActivePane(); active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE); + + char project_root[512]; + realpath(argv[1], project_root); + char *slash = strrchr(project_root, '/'); + if (slash) *slash = '\0'; + + appDebug("peoject root : %s\n", project_root); + + lspStart(E.lsp_client, project_root); + struct buffer_t *buf = &E.buffers[active->buffer_id]; + lspDidOpen(E.lsp_client, buf); + } else { strcat(splash_screen, getenv("HOME")); strcat(splash_screen, "/.beluga/assets/beluga.txt"); @@ -38,11 +58,16 @@ int main(int argc, char *argv[]) { appDebug("splash : %s\n", splash_screen); EditorPane *active = splitScreenGetActivePane(); active->buffer_id = bufferCreate(splash_screen, READ_ONLY); + struct buffer_t *buf = &E.buffers[active->buffer_id]; + lspStart(E.lsp_client, splash_screen); + lspDidOpen(E.lsp_client, buf); + } free(splash_screen); editorSetStatusMessage("HELP: Ctrl-x Ctrl-s = save | Ctrl-x Ctrl-c = quit"); + appDebug("allocation : %d\n", beluga_alloc_counter); while (1) { editorRefreshScreen(); diff --git a/meson.build b/meson.build index 9fec757..7071c96 100644 --- a/meson.build +++ b/meson.build @@ -6,7 +6,8 @@ project('beluga', 'c', ) cc = meson.get_compiler('c') -m = cc.find_library('m', required: false) +m = cc.find_library('m', required: false) +thread_dep = dependency('threads') # Source files src_files = files( @@ -23,11 +24,15 @@ src_files = files( 'src/builtins.c', 'src/buffer.c', 'src/split_screen.c', - 'src/utf8.c' + 'src/utf8.c', + 'src/completion.c', + 'src/lsp_ui.c', + 'src/cJSON.c', + 'src/utils.c' ) # Executable executable('beluga', src_files, - dependencies: [m] + dependencies: [m, thread_dep] ) diff --git a/src/append_buffer.c b/src/append_buffer.c index 38cdc05..1f46062 100644 --- a/src/append_buffer.c +++ b/src/append_buffer.c @@ -1,7 +1,8 @@ #include "../include/append_buffer.h" +#include "include/utils.h" void abAppend(struct abuf *ab, const char *s, int len) { - char *new = realloc(ab->b, ab->len + len); + char *new = bRealloc(ab->b, ab->len + len); if (new == NULL) { return; @@ -11,4 +12,4 @@ void abAppend(struct abuf *ab, const char *s, int len) { ab->len += len; } -void abFree(struct abuf *ab) { free(ab->b); } +void abFree(struct abuf *ab) { bFree(ab->b); } diff --git a/src/buffer.c b/src/buffer.c index a73b811..7ac566f 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -10,10 +10,12 @@ #include "include/split_screen.h" #include #include +#include #include #include #include "include/input.h" +#include "include/utils.h" /** @@ -55,18 +57,18 @@ struct buffer_t* bufferFindById(int buffer_id) * @brief Creates a new buffer for a file * @details Allocates buffer slot, loads file content, saves buffer metadata. * If file is already open, switches to existing buffer instead. - * @param filename Path to the file * @return Buffer ID on success, -1 on failure */ -int bufferCreate(const char* filename, enum bufferStatus_e state) +int bufferCreate(const char* path, enum bufferStatus_e state) { + char *filename = basename((char *) path); // Check if file is already open - const int existing_id = bufferFindByFilename(filename); + const int existing_id = bufferFindByFilename(path); + path = dirname((char *) path); if (existing_id != -1) { return bufferSwitch(existing_id); } - // Check if we have space for more buffers if (E.number_of_buffer >= 64) { @@ -83,6 +85,7 @@ int bufferCreate(const char* filename, enum bufferStatus_e state) new_buf->x = 0; new_buf->y = 0; new_buf->dirty = 0; // New file starts clean + new_buf->path = strdup(path); // Load file content using existing editorOpen editorOpen(new_buf); @@ -161,7 +164,7 @@ int bufferClose(int buffer_id) } // Free buffer resources - free(buf->filename); + bFree(buf->filename); buf->filename = NULL; buf->buffer_id = -1; @@ -297,7 +300,7 @@ void bufferFind(struct buffer_t* buf) break; } } - free(query); + bFree(query); } void bufferFindReverse(struct buffer_t* buf) @@ -320,14 +323,14 @@ void bufferFindReverse(struct buffer_t* buf) break; } } - free(query); + bFree(query); } void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) { if (at < 0 || at > buffer->numrows) return; - row_t *tmp = realloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1)); + row_t *tmp = bRealloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1)); if (!tmp) return; buffer->row = tmp; @@ -338,9 +341,9 @@ void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) { sizeof(row_t) * (buffer->numrows - at)); /* not -at+1 */ } - buffer->row[at].size = len; - buffer->row[at].cap = len + 1; - buffer->row[at].chars = malloc(len + 1); + buffer->row[at].size = (int) len; + buffer->row[at].cap = (int) len + 1; + buffer->row[at].chars = bAlloc(len + 1); if (!buffer->row[at].chars) return; memcpy(buffer->row[at].chars, s, len); @@ -350,7 +353,7 @@ void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) { buffer->dirty++; } -void bufferFreeRow(row_t *row) { free(row->chars); } +void bufferFreeRow(row_t *row) { bFree(row->chars); } /** * \fn editorRowInsertChar(erow *row, int at, int c) @@ -362,12 +365,12 @@ void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at, return; if (row->size + n + 1 > row->cap) { row->cap = (row->size + n + 1) * 2; - row->chars = realloc(row->chars, row->cap); + row->chars = bRealloc(row->chars, row->cap); } memmove(row->chars + at + n, row->chars + at, row->size - at); memcpy(row->chars + at, src, n); row->size += n; - row->chars = realloc(row->chars, row->size + 2); + row->chars = bRealloc(row->chars, row->size + 2); ++buffer->dirty; } @@ -400,6 +403,7 @@ void bufferInsertBytes(const char* src, int n) } bufferRowInsertBytes(buf, &buf->row[buf->y], buf->x, src, n); buf->x += n; + buf->b_has_changed = 1; } void bufferDelBytes(void) @@ -430,7 +434,7 @@ void bufferDelBytes(void) int prev_char_count = editorRowCharCount(prev, prev->size); bufferRowInsertBytes(buf, prev, prev->size, r->chars, r->size); - free(r->chars); + bFree(r->chars); r->chars = NULL; memmove(&buf->row[buf->y], @@ -462,7 +466,7 @@ void bufferInsertNewLine(void) { /* Insert the tail (from cursor to end) as a new row below */ bufferInsertRow(buf, buf->y + 1, &row->chars[buf->x], row->size - buf->x); - /* Re-fetch: realloc inside bufferInsertRow may have moved the array */ + /* Re-fetch: bRealloc inside bufferInsertRow may have moved the array */ row = &buf->row[buf->y]; /* Truncate the current row at the cursor */ diff --git a/src/builtins.c b/src/builtins.c index 810d64b..55206cf 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -20,6 +20,9 @@ #include #include +#include "include/completion.h" +#include "include/utils.h" + /** * @brief Finds a prefix configuration by name * @details Searches the prefix array for a prefix matching the given name. @@ -28,14 +31,17 @@ * @return Matching prefix_t structure, or E.prefix[0] if not found * @note Updates global editor state E */ -struct prefix_t find_prefix(const char prefix_name[64]) { - int i = E.number_of_prefix + 1; - while (i--) { - if (!strcmp(prefix_name, E.prefix[i].prefix_name)) { - return E.prefix[i]; +struct prefix_t find_prefix(const char prefix_name[64]) +{ + int i = E.number_of_prefix + 1; + while (i--) + { + if (!strcmp(prefix_name, E.prefix[i].prefix_name)) + { + return E.prefix[i]; + } } - } - return E.prefix[0]; + return E.prefix[0]; } /** @@ -51,32 +57,37 @@ struct prefix_t find_prefix(const char prefix_name[64]) { * @return lisp_null() * @note Updates global editor state E (key_binds array) */ -Lisp mapKey(Lisp args, LispError *e, LispContext ctx) { - /* - * 3 arguments keybind command prefix - */ - const char *key_sequence = lisp_string(lisp_car(args)); - args = lisp_cdr(args); - // second argument - const Lisp func = lisp_car(args); +Lisp mapKey(Lisp args, LispError* e, LispContext ctx) +{ + /* + * 3 arguments keybind command prefix + */ + const char* key_sequence = lisp_string(lisp_car(args)); + void* memory_temp; + args = lisp_cdr(args); + // second argument + const Lisp func = lisp_car(args); - E.key_binds = (struct keyBind_t *)realloc( - E.key_binds, ++E.number_of_keybinds * sizeof(struct keyBind_t)); - E.key_binds[E.number_of_keybinds - 1].key_sequence = - (char *)malloc(50 * sizeof(char)); + memory_temp = (void*)bRealloc( + E.key_binds, ++E.number_of_keybinds * sizeof(struct keyBind_t)); + E.key_binds = (struct keyBind_t*)memory_temp; + if (!E.key_binds) + editorQuit(args, e, ctx); + E.key_binds[E.number_of_keybinds - 1].key_sequence = + (char*)bAlloc(50 * sizeof(char)); - strncpy(E.key_binds[E.number_of_keybinds - 1].key_sequence, key_sequence, 50); + strncpy(E.key_binds[E.number_of_keybinds - 1].key_sequence, key_sequence, 50); - E.key_binds[E.number_of_keybinds - 1].command = func; + E.key_binds[E.number_of_keybinds - 1].command = func; - // Third argument - args = lisp_cdr(args); - const char *prefix_name = lisp_string(lisp_car(args)); - struct prefix_t prefix = find_prefix(prefix_name); + // Third argument + args = lisp_cdr(args); + const char* prefix_name = lisp_string(lisp_car(args)); + struct prefix_t prefix = find_prefix(prefix_name); - E.key_binds[E.number_of_keybinds - 1].prefix_id = prefix.prefix_id; + E.key_binds[E.number_of_keybinds - 1].prefix_id = prefix.prefix_id; - return lisp_null(); + return lisp_null(); } /** @@ -91,54 +102,62 @@ Lisp mapKey(Lisp args, LispError *e, LispContext ctx) { * @note Updates global editor state E * @see editorMoveCursor() */ -Lisp moveCursor(Lisp args, LispError *e, LispContext ctx) { - const char *direction = lisp_string(lisp_car(args)); - int is_in = 0; - switch (direction[0]) { - case 'u': - is_in = editorMoveCursor(ARROW_UP); - break; - case 'd': - is_in = editorMoveCursor(ARROW_DOWN); - break; - case 'r': - is_in = editorMoveCursor(ARROW_RIGHT); - break; - case 'l': - is_in = editorMoveCursor(ARROW_LEFT); - break; - } - appDebug("move lisp %d\n", is_in); - return lisp_make_bool(is_in); +Lisp moveCursor(Lisp args, LispError* e, LispContext ctx) +{ + const char* direction = lisp_string(lisp_car(args)); + int is_in = 0; + switch (direction[0]) + { + case 'u': + is_in = editorMoveCursor(ARROW_UP); + break; + case 'd': + is_in = editorMoveCursor(ARROW_DOWN); + break; + case 'r': + is_in = editorMoveCursor(ARROW_RIGHT); + break; + case 'l': + is_in = editorMoveCursor(ARROW_LEFT); + break; + default: + break; + } + appDebug("move lisp %d\n", is_in); + return lisp_make_bool(is_in); } /** - * @brief Frees all dynamically allocated editor structures + * @brief bFrees all dynamically allocated editor structures * @details Releases memory for prefix table, keybinds, filename, and all rows. * Called during shutdown to prevent memory leaks. * @note Updates global editor state E */ -void free_structs(void) { - int i, j; - free(E.prefix); - for (i = 0; i < E.number_of_keybinds; ++i) { - free(E.key_binds[i].key_sequence); - } - free(E.key_binds); - // free layout - free(E.layout.panes); - - // Free buffers - for (i = 0; i < E.number_of_buffer; ++i) { - free(E.buffers[i].filename); - for (j = 0; j < E.buffers[i].numrows; ++j) { - free(E.buffers[i].row[j].chars); +void bFree_structs(void) +{ + int i, j; + bFree(E.prefix); + for (i = 0; i < E.number_of_keybinds; ++i) + { + bFree(E.key_binds[i].key_sequence); } - free(E.buffers[i].row); - } + bFree(E.key_binds); + // bFree layout + bFree(E.layout.panes); - free(E.init_file_path); - fclose(E.fd_init_file); + // bFree buffers + for (i = 0; i < E.number_of_buffer; ++i) + { + bFree(E.buffers[i].filename); + for (j = 0; j < E.buffers[i].numrows; ++j) + { + bFree(E.buffers[i].row[j].chars); + } + bFree(E.buffers[i].row); + } + + bFree(E.init_file_path); + fclose(E.fd_init_file); } /** @@ -152,23 +171,25 @@ void free_structs(void) { * @return lisp_null() (never returns on successful exit) * @note Calls exit(0) to terminate program * @note Updates quit_times_buffer counter - * @see free_structs() + * @see bFree_structs() */ -Lisp editorQuit(Lisp args, LispError *e, LispContext ctx) { - if (E.dirty && E.quit_times_buffer > 0) { - editorSetStatusMessage("WARNING! Changes hasn't been saved. Press Ctrl-Q " - "another time to quit."); - --E.quit_times_buffer; - return lisp_null(); - } - free_structs(); - write(STDOUT_FILENO, "\x1b[2J", 4); - write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); - disableRawMode(); - lisp_shutdown(E.ctx); - exit(0); - - return lisp_null(); +Lisp editorQuit(Lisp args, LispError* e, LispContext ctx) +{ + if (E.dirty && E.quit_times_buffer > 0) + { + editorSetStatusMessage("WARNING! Changes hasn't been saved. Press Ctrl-Q " + "another time to quit."); + --E.quit_times_buffer; + return lisp_null(); + } + bFree_structs(); + write(STDOUT_FILENO, "\x1b[2J", 4); + write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); + disableRawMode(); + lspShutdown(E.lsp_client); + lisp_shutdown(E.ctx); + appDebug("Rest alloc %d\n", beluga_alloc_counter); + exit(0); } /** @@ -181,20 +202,24 @@ Lisp editorQuit(Lisp args, LispError *e, LispContext ctx) { * @see editorSave() */ -Lisp l_editorSplitScreenVertical(Lisp args, LispError *e, LispContext ctx) { - if (E.number_of_buffer >= 2) { - splitScreenVertical(E.buffers[0].buffer_id, E.buffers[1].buffer_id); - } else { - editorSetStatusMessage("Need at least 2 buffers open"); - } - return lisp_null(); +Lisp l_editorSplitScreenVertical(Lisp args, LispError* e, LispContext ctx) +{ + if (E.number_of_buffer >= 2) + { + splitScreenVertical(E.buffers[0].buffer_id, E.buffers[1].buffer_id); + } + else + { + editorSetStatusMessage("Need at least 2 buffers open"); + } + return lisp_null(); } -Lisp l_editorSave(Lisp args, LispError *e, LispContext ctx) { +Lisp l_editorSave(Lisp args, LispError* e, LispContext ctx) +{ + editorSave(); - editorSave(); - - return lisp_null(); + return lisp_null(); } /** @@ -206,11 +231,11 @@ Lisp l_editorSave(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @see editorInsertNewLine() */ -Lisp l_editorInsertNewLine(Lisp args, LispError *e, LispContext ctx) { +Lisp l_editorInsertNewLine(Lisp args, LispError* e, LispContext ctx) +{ + bufferInsertNewLine(); - bufferInsertNewLine(); - - return lisp_null(); + return lisp_null(); } /** @@ -222,12 +247,14 @@ Lisp l_editorInsertNewLine(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @note Uses E.constantes.TAB_LENGTH for indentation width */ -Lisp l_editorInserTab(Lisp args, LispError *e, LispContext ctx) { - for (int i = 0; i < E.constantes.TAB_LENGTH; ++i) { - bufferInsertBytes(" ", 1); - } +Lisp l_editorInserTab(Lisp args, LispError* e, LispContext ctx) +{ + for (int i = 0; i < E.constantes.TAB_LENGTH; ++i) + { + bufferInsertBytes(" ", 1); + } - return lisp_null(); + return lisp_null(); } /** @@ -239,11 +266,12 @@ Lisp l_editorInserTab(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @note Updates global editor state E */ -Lisp moveCursorBeginLine(Lisp args, LispError *e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buf = bufferFindById(active->buffer_id); - buf->x = 0; - return lisp_null(); +Lisp moveCursorBeginLine(Lisp args, LispError* e, LispContext ctx) +{ + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buf = bufferFindById(active->buffer_id); + buf->x = 0; + return lisp_null(); } /** @@ -255,11 +283,12 @@ Lisp moveCursorBeginLine(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @note Updates global editor state E */ -Lisp moveCursorEndLine(Lisp args, LispError *e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buf = bufferFindById(active->buffer_id); - buf->x = buf->row[buf->y].size; - return lisp_null(); +Lisp moveCursorEndLine(Lisp args, LispError* e, LispContext ctx) +{ + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buf = bufferFindById(active->buffer_id); + buf->x = buf->row[buf->y].size; + return lisp_null(); } /** @@ -271,9 +300,10 @@ Lisp moveCursorEndLine(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @see editorDelChar() */ -Lisp deletePreviousChar(Lisp args, LispError *e, LispContext ctx) { - bufferDelBytes(); - return lisp_null(); +Lisp deletePreviousChar(Lisp args, LispError* e, LispContext ctx) +{ + bufferDelBytes(); + return lisp_null(); } /** @@ -287,14 +317,16 @@ Lisp deletePreviousChar(Lisp args, LispError *e, LispContext ctx) { * @note Updates global editor state E * @see editorMoveCursor() */ -Lisp editorMoveCursorPageUp(Lisp args, LispError *e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - active->cursor_y = active->y_offset; - int times = E.screenrows; - while (--times) { - editorMoveCursor(ARROW_UP); - } - return lisp_null(); +Lisp editorMoveCursorPageUp(Lisp args, LispError* e, LispContext ctx) +{ + EditorPane* active = splitScreenGetActivePane(); + active->cursor_y = active->y_offset; + int times = E.screenrows; + while (--times) + { + editorMoveCursor(ARROW_UP); + } + return lisp_null(); } /** @@ -308,19 +340,22 @@ Lisp editorMoveCursorPageUp(Lisp args, LispError *e, LispContext ctx) { * @note Updates global editor state E * @see editorMoveCursor() */ -Lisp editorMoveCursorPageDown(Lisp args, LispError *e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - active->cursor_y = active->y_offset + E.screenrows - 1; - if (active->cursor_y > buffer->numrows) { - active->cursor_y = buffer->numrows; - } - int times = E.screenrows; - while (--times) { - editorMoveCursor(ARROW_DOWN); - } +Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx) +{ + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + active->cursor_y = active->y_offset + E.screenrows - 1; + if (active->cursor_y > buffer->numrows) + { + active->cursor_y = buffer->numrows; + } + int times = E.screenrows; + while (--times) + { + editorMoveCursor(ARROW_DOWN); + } - return lisp_null(); + return lisp_null(); } /** @@ -334,16 +369,18 @@ Lisp editorMoveCursorPageDown(Lisp args, LispError *e, LispContext ctx) { * @see editorOpen() * @see editorPrompt() */ -Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) { - char *filename = editorPrompt("Open : %s", getenv("PWD"), 1); - if (filename) { - // editorOpen(filename); - EditorPane *active = splitScreenGetActivePane(); - active->buffer_id = bufferCreate(filename, READ_AND_WRITE); - } - free(filename); +Lisp editorOpenFile(Lisp args, LispError* e, LispContext ctx) +{ + char* filename = editorPrompt("Open : %s", getenv("PWD"), 1); + if (filename) + { + // editorOpen(filename); + EditorPane* active = splitScreenGetActivePane(); + active->buffer_id = bufferCreate(filename, READ_AND_WRITE); + } + bFree(filename); - return lisp_null(); + return lisp_null(); } /** @@ -356,10 +393,11 @@ Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @note Uses first character of the string argument */ -Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx) { - const char *src = lisp_string(lisp_car(args)); - bufferInsertBytes(src, strlen(src)); - return lisp_null(); +Lisp editorPrintC(Lisp args, LispError* e, LispContext ctx) +{ + const char* src = lisp_string(lisp_car(args)); + bufferInsertBytes(src, (int) strlen(src)); + return lisp_null(); } /** @@ -373,46 +411,52 @@ Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx) { * @note Package files must be valid Lisp code * @note Package directory defaults to ~/.beluga/packages/ */ -Lisp addPackage(Lisp args, LispError *e, LispContext ctx) { - const char *package_name = lisp_string(lisp_car(args)); - appDebug("%s\n", package_name); - char *package_dir = (char *)calloc(256, sizeof(char)); - FILE *fd_package = NULL; - strcat(package_dir, getenv("HOME")); - strcat(package_dir, "/.beluga/packages/"); - strcat(package_dir, package_name); - strcat(package_dir, "/init.lisp"); - appDebug("%s\n", package_dir); - fd_package = fopen(package_dir, "r"); - lisp_eval(lisp_read_file(fd_package, &E.ctx_error, E.ctx), &E.ctx_error, - E.ctx); - fclose(fd_package); - free(package_dir); +Lisp addPackage(Lisp args, LispError* e, LispContext ctx) +{ + const char* package_name = lisp_string(lisp_car(args)); + appDebug("%s\n", package_name); + char* package_dir = (char*)calloc(256, sizeof(char)); + FILE* fd_package = NULL; + strcat(package_dir, getenv("HOME")); + strcat(package_dir, "/.beluga/packages/"); + strcat(package_dir, package_name); + strcat(package_dir, "/init.lisp"); + appDebug("%s\n", package_dir); + fd_package = fopen(package_dir, "r"); + lisp_eval(lisp_read_file(fd_package, &E.ctx_error, E.ctx), &E.ctx_error, + E.ctx); + fclose(fd_package); + bFree(package_dir); - return lisp_null(); + return lisp_null(); } -Lisp editorSwitchNextBuffer(Lisp args, LispError *e, LispContext ctx) { - appDebug("switch buffer\n"); - if (E.number_of_buffer > 0) { - EditorPane *active = splitScreenGetActivePane(); - int next_idx = (active->buffer_id + 1) % E.number_of_buffer; - active->buffer_id = next_idx; - } +Lisp editorSwitchNextBuffer(Lisp args, LispError* e, LispContext ctx) +{ + appDebug("switch buffer\n"); + if (E.number_of_buffer > 0) + { + EditorPane* active = splitScreenGetActivePane(); + int next_idx = (active->buffer_id + 1) % E.number_of_buffer; + active->buffer_id = next_idx; + } - return lisp_null(); + return lisp_null(); } -Lisp editorSwitchNextPane(Lisp args, LispError *e, LispContext ctx) { - splitScreenSwitchPane(); - return lisp_null(); +Lisp editorSwitchNextPane(Lisp args, LispError* e, LispContext ctx) +{ + splitScreenSwitchPane(); + return lisp_null(); } -Lisp editorUnifiedPanes(Lisp args, LispError *e, LispContext ctx) { - if (E.layout.num_panes - 1) { - splitScreenUnify(); - } - return lisp_null(); +Lisp editorUnifiedPanes(Lisp args, LispError* e, LispContext ctx) +{ + if (E.layout.num_panes - 1) + { + splitScreenUnify(); + } + return lisp_null(); } /** @@ -424,12 +468,13 @@ Lisp editorUnifiedPanes(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @see editorFind() */ -Lisp bufferFind_L(Lisp args, LispError *e, LispContext ctx) { - appDebug("LispFind\n"); - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - bufferFind(buffer); - return lisp_null(); +Lisp bufferFind_L(Lisp args, LispError* e, LispContext ctx) +{ + appDebug("LispFind\n"); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + bufferFind(buffer); + return lisp_null(); } /** @@ -441,12 +486,13 @@ Lisp bufferFind_L(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @see editorFind() */ -Lisp bufferFindReverse_L(Lisp args, LispError *e, LispContext ctx) { - appDebug("LispFind\n"); - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - bufferFindReverse(buffer); - return lisp_null(); +Lisp bufferFindReverse_L(Lisp args, LispError* e, LispContext ctx) +{ + appDebug("LispFind\n"); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + bufferFindReverse(buffer); + return lisp_null(); } /** @@ -458,16 +504,20 @@ Lisp bufferFindReverse_L(Lisp args, LispError *e, LispContext ctx) { * @param ctx Lisp context * @return Lisp character object representing the character at cursor */ -Lisp editorReadChar_L(Lisp args, LispError *e, LispContext ctx) { - Lisp returned_char; - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - if (buffer->row[buffer->y].chars[buffer->x] == 0) { - returned_char = lisp_make_char('a'); - } else { - returned_char = lisp_make_char(buffer->row[buffer->y].chars[buffer->x]); - } - return returned_char; +Lisp editorReadChar_L(Lisp args, LispError* e, LispContext ctx) +{ + Lisp returned_char; + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + if (buffer->row[buffer->y].chars[buffer->x] == 0) + { + returned_char = lisp_make_char('a'); + } + else + { + returned_char = lisp_make_char(buffer->row[buffer->y].chars[buffer->x]); + } + return returned_char; } /** @@ -481,17 +531,18 @@ Lisp editorReadChar_L(Lisp args, LispError *e, LispContext ctx) { * @note Updates global editor state E (prefix_state) * @see find_prefix() */ -Lisp editorSetPrefix(Lisp args, LispError *e, LispContext ctx) { - /* - * Set the prefix state of editor to the prefix in argument - */ - const char *prefix_name = lisp_string(lisp_car(args)); - struct prefix_t prefix = find_prefix(prefix_name); - E.prefix_state = prefix.prefix_id; - editorSetStatusMessage("prefix %s", prefix.prefix_name); - appDebug("%s set\n", prefix_name); +Lisp editorSetPrefix(Lisp args, LispError* e, LispContext ctx) +{ + /* + * Set the prefix state of editor to the prefix in argument + */ + const char* prefix_name = lisp_string(lisp_car(args)); + struct prefix_t prefix = find_prefix(prefix_name); + E.prefix_state = prefix.prefix_id; + editorSetStatusMessage("prefix %s", prefix.prefix_name); + appDebug("%s set\n", prefix_name); - return lisp_null(); + return lisp_null(); } /** @@ -504,57 +555,93 @@ Lisp editorSetPrefix(Lisp args, LispError *e, LispContext ctx) { * @return lisp_null() * @note Updates global editor state E (prefix array) */ -Lisp editorPrefix(Lisp args, LispError *e, LispContext ctx) { - E.prefix = (struct prefix_t *)realloc(E.prefix, (++(E.number_of_prefix) + 1) * - sizeof(struct prefix_t)); - E.prefix[E.number_of_prefix].prefix_id = E.number_of_prefix; - strncpy(E.prefix[E.number_of_prefix].prefix_name, lisp_string(lisp_car(args)), - 64); - return lisp_null(); +Lisp editorPrefix(Lisp args, LispError* e, LispContext ctx) +{ + void * memory_temp; + E.prefix = (struct prefix_t*)bRealloc(E.prefix, (++(E.number_of_prefix) + 1) * + sizeof(struct prefix_t)); + E.prefix[E.number_of_prefix].prefix_id = E.number_of_prefix; + strncpy(E.prefix[E.number_of_prefix].prefix_name, lisp_string(lisp_car(args)), + 64); + return lisp_null(); } -Lisp editorPaste(Lisp args, LispError *e, LispContext ctx) +Lisp editorPaste(Lisp args, LispError* e, LispContext ctx) { - const char *char_to_paste = editorGetClipboard(); - appDebug("editor-paste, %s\n", char_to_paste); - bufferInsertBytes(char_to_paste, (int) strlen(char_to_paste)); - return lisp_null(); + const char* char_to_paste = editorGetClipboard(); + appDebug("editor-paste, %s\n", char_to_paste); + bufferInsertBytes(char_to_paste, (int)strlen(char_to_paste)); + return lisp_null(); } -Lisp editorCutEndLine(Lisp args, LispError *e, LispContext ctx) +Lisp editorCutEndLine(Lisp args, LispError* e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - int bytes_to_delete = buffer->row[buffer->y].size - buffer->x; - editorSetClipboard(&buffer->row[buffer->y].chars[buffer->x], bytes_to_delete); - buffer->x = buffer->row[buffer->y].size; - while (bytes_to_delete--) - { - bufferDelBytes(); - } - return lisp_null(); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + int bytes_to_delete = editorRowCharCount(&buffer->row[buffer->y], + buffer->row[buffer->y].chars[buffer->row[buffer->y].size]) - + editorRowCharCount(&buffer->row[buffer->y], buffer->x); + editorSetClipboard(&buffer->row[buffer->y].chars[buffer->x], bytes_to_delete); + buffer->x = buffer->row[buffer->y].size; + while (bytes_to_delete--) + { + bufferDelBytes(); + } + return lisp_null(); } -Lisp editorMoveBegBuffer(Lisp args, LispError *e, LispContext ctx) +Lisp editorMoveBegBuffer(Lisp args, LispError* e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - buffer->x = 0; - buffer->y = 0; - return lisp_null(); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + buffer->x = 0; + buffer->y = 0; + return lisp_null(); } -Lisp editorMoveEndBuffer(Lisp args, LispError *e, LispContext ctx) +Lisp editorMoveEndBuffer(Lisp args, LispError* e, LispContext ctx) { - EditorPane *active = splitScreenGetActivePane(); - struct buffer_t *buffer = bufferFindById(active->buffer_id); - buffer->x = buffer->row[buffer->numrows-1].size; - buffer->y = buffer->numrows-1; - return lisp_null(); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + buffer->x = buffer->row[buffer->numrows - 1].size; + buffer->y = buffer->numrows - 1; + return lisp_null(); } -Lisp editorAutoComplete(Lisp args, LispError *e, LispContext ctx) +Lisp editorAutoComplete(Lisp args, LispError* e, LispContext ctx) { - // createContextBuffer(E.cursor_x - 2, E.cursor_y + 1, "hello"); - return lisp_null(); + // createContextBuffer(E.cursor_x - 2, E.cursor_y + 1, "hello"); + appDebug("editor-auto-complete\n"); + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buffer = bufferFindById(active->buffer_id); + E.lsp_completion.visible = 1; + lspRequestCompletion( + E.lsp_client, + buffer, + active->cursor_y + active->y_offset, // file line + active->cursor_x + active->x_offset, // file col + active->cursor_x + active->origin_x + GUTTER_WIDTH, // screen x + active->cursor_y + active->origin_y // screen y + ); + return lisp_null(); +} + +Lisp lspDefinition(Lisp args, LispError* e, LispContext ctx) +{ + (void)args; + (void)e; + (void)ctx; + if (!E.lsp_client || E.lsp_client->state != LSP_READY) + return lisp_null(); + + EditorPane* active = splitScreenGetActivePane(); + struct buffer_t* buf = bufferFindById(active->buffer_id); + if (!buf) return lisp_null(); + + lspRequestDefinition( + E.lsp_client, buf, + active->cursor_y + active->y_offset, + active->cursor_x + active->x_offset + ); + return lisp_null(); } diff --git a/src/cJSON.c b/src/cJSON.c new file mode 100644 index 0000000..f861fb2 --- /dev/null +++ b/src/cJSON.c @@ -0,0 +1,3206 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "../include/cJSON.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#ifdef _WIN32 +#define NAN sqrt(-1.0) +#else +#define NAN 0.0/0.0 +#endif +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +{ + if (!cJSON_IsString(item)) + { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_IsNumber(item)) + { + return (double) NAN; + } + + return item->valuedouble; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 19) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_CDECL internal_malloc(size_t size) +{ + return malloc(size); +} +static void CJSON_CDECL internal_free(void *pointer) +{ + free(pointer); +} +static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + item->valuestring = NULL; + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + item->string = NULL; + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char *number_c_string; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + size_t number_string_length = 0; + cJSON_bool has_decimal_point = false; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_string_length++; + break; + + case '.': + number_string_length++; + has_decimal_point = true; + break; + + default: + goto loop_end; + } + } +loop_end: + /* malloc for temporary buffer, add 1 for '\0' */ + number_c_string = (unsigned char *) input_buffer->hooks.allocate(number_string_length + 1); + if (number_c_string == NULL) + { + return false; /* allocation failure */ + } + + memcpy(number_c_string, buffer_at_offset(input_buffer), number_string_length); + number_c_string[number_string_length] = '\0'; + + if (has_decimal_point) + { + for (i = 0; i < number_string_length; i++) + { + if (number_c_string[i] == '.') + { + /* replace '.' with the decimal point of the current locale (for strtod) */ + number_c_string[i] = decimal_point; + } + } + } + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (object == NULL) + { + return (double)NAN; + } + + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +/* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + size_t v1_len; + size_t v2_len; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + /* return NULL if the object is corrupted or valuestring is NULL */ + if (object->valuestring == NULL || valuestring == NULL) + { + return NULL; + } + + v1_len = strlen(valuestring); + v2_len = strlen(object->valuestring); + + if (v1_len <= v2_len) + { + /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */ + if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring )) + { + return NULL; + } + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = sprintf((char*)number_buffer, "null"); + } + else if(d == (double)item->valueint) + { + length = sprintf((char*)number_buffer, "%d", item->valueint); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + output = NULL; + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } + + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + if (printed != NULL) + { + hooks->deallocate(printed); + printed = NULL; + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + p.buffer = NULL; + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((length < 0) || (buffer == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + if (output_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* nesting is too deep */ + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + if (cannot_access_at_index(input_buffer, 1)) + { + goto fail; /* nothing comes after the comma */ + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + if (output_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* nesting is too deep */ + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + return add_item_to_array(array, item); +} + +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void* cast_away_const(const void* string) +{ + return (void*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + + +static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) +{ + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } + + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +{ + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +{ + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +{ + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +{ + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +{ + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +{ + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +{ + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +{ + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +{ + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0 || newitem == NULL) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + if (after_inserted != array->child && after_inserted->prev == NULL) { + /* return false if after_inserted is a corrupted array item */ + return false; + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) +{ + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +{ + if (which < 0) + { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +{ + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } + + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +/* Duplication */ +cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse); + +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) +{ + return cJSON_Duplicate_rec(item, 0, recurse ); +} + +cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse) +{ + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + if(depth >= CJSON_CIRCULAR_LIMIT) { + goto fail; + } + newchild = cJSON_Duplicate_rec(child, depth + 1, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } + + return newitem; + +fail: + if (newitem != NULL) + { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) +{ + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) +{ + return global_hooks.allocate(size); +} + +CJSON_PUBLIC(void) cJSON_free(void *object) +{ + global_hooks.deallocate(object); + object = NULL; +} diff --git a/src/completion.c b/src/completion.c new file mode 100644 index 0000000..e1bb6c7 --- /dev/null +++ b/src/completion.c @@ -0,0 +1,555 @@ +// +// Created by Giorgio on 25/05/2026. +// + +#include "../include/completion.h" + +#include +#include +#include +#include + +#include "include/append_buffer.h" +#include "include/cJSON.h" +#include "include/data.h" +#include "include/lsp_ui.h" +#include "include/terminal.h" +#include "include/utils.h" + +void createContextBuffer(const int x, const int y, const char* text) +{ + E.context_buffers = bAlloc(sizeof(ContextBuffer)); + ContextBuffer* buffer = E.context_buffers; + buffer->editor_x = x; + buffer->editor_y = y; + buffer->height = 1; + buffer->rows = bAlloc(sizeof(struct row)); + if (!buffer->rows) return; + buffer->rows[0].chars = strdup(text); + buffer->rows[0].size = strlen(text); + buffer->width = strlen(text); +} + +static void lsp_send(int fd, const char* json) +{ + int body_len = strlen(json); + char header[1024]; + int header_len = snprintf(header, sizeof(header), + "Content-Length: %d\r\n\r\n", body_len); + + // Write header + body atomically in two writes, no dprintf mixing + write(fd, header, header_len); + write(fd, json, body_len); + + // Log to stderr for debugging + fprintf(stderr, "[LSP →] Content-Length: %d | %s\n", body_len, json); + fflush(stderr); +} + +static int lsp_uri_to_buffer_id(const char* uri) +{ + const char *path = uri; + if (strncmp(uri, "file://", 7) == 0) + path = uri + 7; + // path is now "/absolute/path" — realpath output matches this directly + + for (int i = 0; i < E.number_of_buffer; i++) { + if (E.buffers[i].filename == NULL) continue; + char abs[PATH_MAX]; + realpath(E.buffers[i].filename, abs); + fprintf(stderr, "[URI MATCH] comparing '%s' vs '%s'\n", abs, path); + if (strcmp(abs, path) == 0) + return E.buffers[i].buffer_id; + } + return -1; +} + +static char* lsp_recv(int fd) +{ + char header[1024]; + int content_length = 0; + + while (1) + { + int i = 0; + char c; + int n; + // Read until \n + while ((n = read(fd, &c, 1)) == 1 && c != '\n') + header[i++] = c; + if (n <= 0) return NULL; + header[i] = '\0'; + // Strip \r explicitly + while (i > 0 && (header[i - 1] == '\r' || header[i - 1] == ' ')) + header[--i] = '\0'; + if (i == 0) break; // blank line = end of headers + if (strncmp(header, "Content-Length: ", 16) == 0) + content_length = atoi(header + 16); + } + + if (content_length == 0) return NULL; + + char* body = bAlloc(content_length + 1); + int total = 0; + while (total < content_length) + { + int n = read(fd, body + total, content_length - total); + if (n <= 0) + { + bFree(body); + return NULL; + } + total += n; + } + body[content_length] = '\0'; + return body; +} + +static void lsp_dispatch(LspClient* lsp, const char* json) +{ + if (!json) return; + + cJSON* root = cJSON_Parse(json); + if (!root) + { + fprintf(stderr, "[LSP ←] Failed to parse JSON: %.120s\n", json); + return; + } + + cJSON* method = cJSON_GetObjectItem(root, "method"); + cJSON* id = cJSON_GetObjectItem(root, "id"); + cJSON* result = cJSON_GetObjectItem(root, "result"); + cJSON* error = cJSON_GetObjectItem(root, "error"); + + // ── Error response ──────────────────────────────────────────────────────── + if (error) + { + cJSON* msg = cJSON_GetObjectItem(error, "message"); + fprintf(stderr, "[LSP ←] ERROR: %s\n", + msg ? msg->valuestring : "(no message)"); + cJSON_Delete(root); + return; + } + + // ── Notification (no id, has method) ───────────────────────────────────── + if (method && !id) + { + const char* m = method->valuestring; + fprintf(stderr, "[LSP ←] NOTIF: %s\n", m); + + if (strcmp(m, "textDocument/publishDiagnostics") == 0) + { + // Find which buffer this diagnostic belongs to + cJSON* params = cJSON_GetObjectItem(root, "params"); + cJSON* uri = cJSON_GetObjectItem(params, "uri"); + int buf_id = lsp_uri_to_buffer_id( + uri ? uri->valuestring : ""); + + fprintf(stderr, "[LSP ←] Diagnostics for buffer %d\n", buf_id); + + pthread_mutex_lock(&lsp->lock); + lspParseDiagnostics(json, &E.lsp_diagnostics, buf_id); + pthread_mutex_unlock(&lsp->lock); + write(lsp->wake_pipe[1], "d", 1); + } + + else if (strcmp(m, "window/logMessage") == 0) + { + cJSON* params = cJSON_GetObjectItem(root, "params"); + cJSON* message = cJSON_GetObjectItem(params, "message"); + fprintf(stderr, "[LSP ←] LOG: %s\n", + message ? message->valuestring : ""); + } + E.lsp_client->completion_just_arrived = 1; + + + cJSON_Delete(root); + return; + } + + // ── Response (has id + result) ──────────────────────────────────────────── + if (id && result) + { + int response_id = id->valueint; + fprintf(stderr, "[LSP ←] RESPONSE id=%d\n", response_id); + + // initialize response → send initialized + mark ready + if (lsp->state == LSP_INITIALIZING) + { + fprintf(stderr, "[LSP ←] Initialize OK, sending initialized\n"); + lsp_send(lsp->write_fd, + "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}"); + + pthread_mutex_lock(&lsp->lock); + lsp->state = LSP_READY; + pthread_cond_signal(&lsp->ready_cond); + pthread_mutex_unlock(&lsp->lock); + E.lsp_client->completion_just_arrived = 1; + + cJSON_Delete(root); + return; + } + + // completion response → parse items and show popup + cJSON* items = cJSON_GetObjectItem(result, "items"); + if (items && cJSON_IsArray(items)) + { + int count = cJSON_GetArraySize(items); + fprintf(stderr, "[LSP ←] Completion: %d items\n", count); + + // Print each item to stderr for debugging + cJSON* item; + int i = 0; + cJSON_ArrayForEach(item, items) + { + cJSON* label = cJSON_GetObjectItem(item, "label"); + cJSON* detail = cJSON_GetObjectItem(item, "detail"); + cJSON* kind = cJSON_GetObjectItem(item, "kind"); + fprintf(stderr, " [%d] kind=%-2d %-40s %s\n", + i++, + kind ? kind->valueint : 0, + label ? label->valuestring : "(no label)", + detail ? detail->valuestring : ""); + if (i >= 10) + { + fprintf(stderr, " ... (%d more)\n", count - 10); + break; + } + } + + pthread_mutex_lock(&lsp->lock); + lspParseCompletion(json, &E.lsp_completion, + lsp->completion_cursor_x, + lsp->completion_cursor_y); + pthread_mutex_unlock(&lsp->lock); + E.lsp_client->completion_just_arrived = 1; + + fprintf(stderr, "[POPUP] visible=%d count=%d origin=(%d,%d)\n", + E.lsp_completion.visible, + E.lsp_completion.count, + E.lsp_completion.origin_x, + E.lsp_completion.origin_y); + write(lsp->wake_pipe[1], "c", 1); + + cJSON_Delete(root); + return; + } + + // definition response → jump to location + cJSON* uri_item = cJSON_GetObjectItem(result, "uri"); + if (uri_item) + { + cJSON* range = cJSON_GetObjectItem(result, "range"); + cJSON* start = cJSON_GetObjectItem(range, "start"); + int line = cJSON_GetObjectItem(start, "line")->valueint; + int col = cJSON_GetObjectItem(start, "character")->valueint; + fprintf(stderr, "[LSP ←] Definition: %s:%d:%d\n", + uri_item->valuestring, line, col); + E.lsp_client->completion_just_arrived = 1; + + // TODO: jump to that location + cJSON_Delete(root); + return; + } + + fprintf(stderr, "[LSP ←] Unhandled response id=%d: %.80s\n", + response_id, json); + } + + cJSON_Delete(root); +} + +static void* lsp_reader(void* arg) +{ + LspClient* lsp = (LspClient*)arg; + while (lsp->state != LSP_SHUTDOWN) + { + char* msg = lsp_recv(lsp->read_fd); + if (!msg) break; // ← pipe closed or error, exit cleanly + lsp_dispatch(lsp, msg); + bFree(msg); + } + return NULL; +} + +// ─── lifecycle ─────────────────────────────────────────────────────────────── + +int lspStart(LspClient* lsp, const char* project_root) +{ + int to_clangd[2], from_clangd[2]; + pipe(to_clangd); + pipe(from_clangd); + + pipe(lsp->wake_pipe); + + lsp->pid = fork(); + if (lsp->pid == 0) + { + // Child: become clangd + dup2(to_clangd[0], STDIN_FILENO); + dup2(from_clangd[1], STDOUT_FILENO); + close(to_clangd[1]); + close(from_clangd[0]); + execlp("clangd", "clangd", "--log=error", "--completion-style=detailed", NULL); + _exit(1); // clangd not found + } + + close(to_clangd[0]); + close(from_clangd[1]); + lsp->write_fd = to_clangd[1]; + lsp->read_fd = from_clangd[0]; + lsp->next_id = 1; + lsp->state = LSP_INITIALIZING; + pthread_mutex_init(&lsp->lock, NULL); + + // Send initialize + char buf[1024]; + snprintf(buf, sizeof(buf), + "{\"jsonrpc\":\"2.0\",\"id\":%d,\"method\":\"initialize\"," + "\"params\":{\"processId\":%d,\"rootUri\":\"file://%s\"," + "\"capabilities\":{" + "\"textDocument\":{" + "\"completion\":{\"completionItem\":{\"snippetSupport\":false}}," + "\"hover\":{}," + "\"definition\":{}," + "\"publishDiagnostics\":{}" + "}" + "}}}", + lsp->next_id++, getpid(), project_root); + + pthread_mutex_init(&lsp->lock, NULL); + pthread_cond_init(&lsp->ready_cond, NULL); + pthread_create(&lsp->reader_thread, NULL, lsp_reader, lsp); + + + lsp_send(lsp->write_fd, buf); + + pthread_mutex_lock(&lsp->lock); + while (lsp->state != LSP_READY) + pthread_cond_wait(&lsp->ready_cond, &lsp->lock); + pthread_mutex_unlock(&lsp->lock); + return 0; +} + +// ─── document sync ─────────────────────────────────────────────────────────── + +// Build the full buffer text into a bAlloc'd string +static char* buffer_to_text(struct buffer_t* buf) +{ + int total = 0; + for (int i = 0; i < buf->numrows; i++) + total += buf->row[i].size + 1; // +1 for \n + char* text = bAlloc(total + 1); + char* p = text; + for (int i = 0; i < buf->numrows; i++) + { + memcpy(p, buf->row[i].chars, buf->row[i].size); + p += buf->row[i].size; + *p++ = '\n'; + } + *p = '\0'; + return text; +} + +void lspDidOpen(LspClient* lsp, struct buffer_t* buf) +{ + if (lsp->state != LSP_READY || buf->b_lsp_open) return; + + char abs[PATH_MAX]; + realpath(buf->filename, abs); + char uri[PATH_MAX + 8]; + snprintf(uri, sizeof(uri), "file://%s", abs); + + const char* lang = "c"; + if (strstr(buf->filename, ".cpp") || strstr(buf->filename, ".cc")) + lang = "cpp"; + + char* raw = buffer_to_text(buf); + + // Let cJSON handle ALL escaping — don't touch the text yourself + cJSON* root = cJSON_CreateObject(); + cJSON* params = cJSON_CreateObject(); + cJSON* td = cJSON_CreateObject(); + + cJSON_AddStringToObject(root, "jsonrpc", "2.0"); + cJSON_AddStringToObject(root, "method", "textDocument/didOpen"); + + cJSON_AddStringToObject(td, "uri", uri); + cJSON_AddStringToObject(td, "languageId", lang); + cJSON_AddNumberToObject(td, "version", 1); + cJSON_AddStringToObject(td, "text", raw); // cJSON escapes this + + cJSON_AddItemToObject(params, "textDocument", td); + cJSON_AddItemToObject(root, "params", params); + + char* msg = cJSON_PrintUnformatted(root); + lsp_send(lsp->write_fd, msg); + + bFree(msg); + bFree(raw); + cJSON_Delete(root); + buf->b_lsp_open = 1; +} + +void lspDidChange(LspClient* lsp, struct buffer_t* buf) +{ + if (lsp->state != LSP_READY || !buf->b_lsp_open) return; + + char abs[PATH_MAX]; + realpath(buf->filename, abs); + char uri[PATH_MAX + 8]; + snprintf(uri, sizeof(uri), "file://%s", abs); + + char* raw = buffer_to_text(buf); + + cJSON* root = cJSON_CreateObject(); + cJSON* params = cJSON_CreateObject(); + cJSON* td = cJSON_CreateObject(); + cJSON* changes = cJSON_CreateArray(); + cJSON* change = cJSON_CreateObject(); + + cJSON_AddStringToObject(root, "jsonrpc", "2.0"); + cJSON_AddStringToObject(root, "method", "textDocument/didChange"); + + cJSON_AddStringToObject(td, "uri", uri); + cJSON_AddNumberToObject(td, "version", buf->dirty); + + cJSON_AddStringToObject(change, "text", raw); // full content sync + cJSON_AddItemToArray(changes, change); + + cJSON_AddItemToObject(params, "textDocument", td); + cJSON_AddItemToObject(params, "contentChanges", changes); + cJSON_AddItemToObject(root, "params", params); + + char* msg = cJSON_PrintUnformatted(root); + lsp_send(lsp->write_fd, msg); + + bFree(msg); + bFree(raw); + cJSON_Delete(root); +} + +void lspDidClose(LspClient* lsp, struct buffer_t* buf) +{ + char abs[PATH_MAX]; + realpath(buf->filename, abs); + char uri[PATH_MAX + 8]; + snprintf(uri, sizeof(uri), "file://%s", abs); + + cJSON* root = cJSON_CreateObject(); + cJSON* params = cJSON_CreateObject(); + cJSON* td = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "jsonrpc", "2.0"); + cJSON_AddStringToObject(root, "method", "textDocument/didClose"); + cJSON_AddStringToObject(td, "uri", uri); // ← fixed + cJSON_AddItemToObject(params, "textDocument", td); + cJSON_AddItemToObject(root, "params", params); + char* msg = cJSON_PrintUnformatted(root); + lsp_send(lsp->write_fd, msg); + bFree(msg); + cJSON_Delete(root); + buf->b_lsp_open = 0; +} + +// ─── requests ──────────────────────────────────────────────────────────────── + +void lspRequestCompletion(LspClient* lsp, struct buffer_t* buf, + int line, int col, + int screen_x, int screen_y) +{ + if (lsp->state != LSP_READY) return; + lsp->completion_cursor_x = screen_x; // ← add + lsp->completion_cursor_y = screen_y; + + appDebug("LSP REQUEST COMP"); + + char* msg; + char abs[PATH_MAX]; + realpath(buf->filename, abs); + char uri[PATH_MAX + 8]; + snprintf(uri, sizeof(uri), "file://%s", abs); + + cJSON* req = cJSON_CreateObject(); + cJSON* params = cJSON_CreateObject(); + cJSON* td = cJSON_CreateObject(); + cJSON* position = cJSON_CreateObject(); + + cJSON_AddStringToObject(req, "jsonrpc", "2.0"); + cJSON_AddNumberToObject(req, "id", lsp->next_id++); + cJSON_AddStringToObject(req, "method", "textDocument/completion"); + + cJSON_AddStringToObject(td, "uri", uri); + + cJSON_AddNumberToObject(position, "line", line); + cJSON_AddNumberToObject(position, "character", col); + cJSON_AddItemToObject(params, "position", position); + + cJSON_AddItemToObject(params, "textDocument", td); + cJSON_AddItemToObject(req, "params", params); + + msg = cJSON_PrintUnformatted(req); + lsp_send(lsp->write_fd, msg); + E.lsp_client->completion_requested = 1; + cJSON_Delete(req); + bFree(msg); +} + +void lspRequestDefinition(LspClient* lsp, struct buffer_t* buf, int line, int col) +{ + if (lsp->state != LSP_READY) return; + char* msg; + asprintf(&msg, + "{\"jsonrpc\":\"2.0\",\"id\":%d,\"method\":\"textDocument/definition\"," + "\"params\":{\"textDocument\":{\"uri\":\"file://%s\"}," + "\"position\":{\"line\":%d,\"character\":%d}}}", + lsp->next_id++, buf->filename, line, col); + lsp_send(lsp->write_fd, msg); + bFree(msg); +} + +void lspShutdown(LspClient* lsp) +{ + if (!lsp || lsp->state == LSP_SHUTDOWN) return; + + lsp->state = LSP_SHUTDOWN; + + // 1. Send shutdown request (clangd expects this before exit) + cJSON* req = cJSON_CreateObject(); + cJSON_AddStringToObject(req, "jsonrpc", "2.0"); + cJSON_AddNumberToObject(req, "id", lsp->next_id++); + cJSON_AddStringToObject(req, "method", "shutdown"); + cJSON_AddNullToObject(req, "params"); + char* msg = cJSON_PrintUnformatted(req); + lsp_send(lsp->write_fd, msg); + bFree(msg); + cJSON_Delete(req); + + // 2. Wait briefly for the shutdown response + // (don't block forever — clangd has 2s to reply) + struct timeval tv = {.tv_sec = 2, .tv_usec = 0}; + fd_set fds; + FD_ZERO(&fds); + FD_SET(lsp->read_fd, &fds); + if (select(lsp->read_fd + 1, &fds, NULL, NULL, &tv) > 0) + { + char* resp = lsp_recv(lsp->read_fd); + bFree(resp); + } + + // 3. Send exit notification + lsp_send(lsp->write_fd, + "{\"jsonrpc\":\"2.0\",\"method\":\"exit\",\"params\":null}"); + + // 4. Close pipes — this signals the reader thread to stop + close(lsp->write_fd); + close(lsp->read_fd); + + // 5. Wait for reader thread to finish + pthread_join(lsp->reader_thread, NULL); + pthread_mutex_destroy(&lsp->lock); + + // 6. Reap the clangd process + waitpid(lsp->pid, NULL, 0); + + bFree(lsp); +} diff --git a/src/editor_op.c b/src/editor_op.c index a028899..32ed498 100644 --- a/src/editor_op.c +++ b/src/editor_op.c @@ -8,6 +8,7 @@ #include "../include/split_screen.h" #include "../include/terminal.h" #include "../include/utf8.h" +#include "include/utils.h" extern struct editorConfig E; @@ -83,13 +84,13 @@ char *editorGetClipboard(void) { size_t cap = 4096; size_t len = 0; - char *buf = malloc(cap); + char *buf = bAlloc(cap); int c; while ((c = fgetc(pipe)) != EOF) { if (len + 1 >= cap) { cap *= 2; - buf = realloc(buf, cap); + buf = bRealloc(buf, cap); } buf[len++] = (char)c; } diff --git a/src/file_io.c b/src/file_io.c index b1ac7d1..42feceb 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -21,6 +21,8 @@ #include #include +#include "../include/utils.h" + extern struct editorConfig E; /** @@ -37,13 +39,13 @@ void editorCloseFile(void) { active->x_offset = 0; active->y_offset = 0; for (int i = 0; i < buf->numrows; ++i) { - free(buf->row[i].chars); + bFree(buf->row[i].chars); } buf->numrows = 0; - free(buf->row); + bFree(buf->row); buf->row = NULL; buf->dirty = 0; - free(buf->filename); + bFree(buf->filename); buf->filename = NULL; E.status_msg[0] = '\0'; E.status_msg_time = 0; @@ -62,8 +64,14 @@ void editorCloseFile(void) { */ void editorOpen(struct buffer_t* buffer) { FILE *fp; + char full_name[1024]; + strcpy(full_name, buffer->path); + strcat(full_name, "/"); + strcat(full_name, buffer->filename); + strcat(full_name, "\0"); + appDebug("full name : %s", full_name); - fp = fopen(buffer->filename, "a+"); + fp = fopen(full_name, "a+"); if (!fp) die("fopen"); @@ -80,10 +88,10 @@ void editorOpen(struct buffer_t* buffer) { } appDebug("line %s\n", line); bufferInsertRow(buffer, buffer->numrows, line, line_len); - free(line); + bFree(line); line = NULL; } - free(line); + bFree(line); fclose(fp); E.dirty = 0; } diff --git a/src/init.c b/src/init.c index 7ff5864..4090cc2 100644 --- a/src/init.c +++ b/src/init.c @@ -11,6 +11,7 @@ #define LISP_IMPLEMENTATION #include "../include/lisp.h" #include "../include/lisp_lib.h" +#include "include/utils.h" struct editorConfig; @@ -50,6 +51,8 @@ void initBuiltins() { registerBuiltin("editor-move-cursor-beg-buffer", editorMoveBegBuffer); registerBuiltin("editor-move-cursor-end-buffer", editorMoveEndBuffer); registerBuiltin("editor-auto-complete", editorAutoComplete); + // registerBuiltin("lsp-complete", lspComplete); + registerBuiltin("lsp-definition", lspDefinition); } void initConfig() { @@ -128,13 +131,14 @@ void initEditor() { E.number_of_keybinds = 0; E.number_of_prefix = 0; // General prefix is 0 (no prefix) - E.prefix = (struct prefix_t *)malloc(sizeof(struct prefix_t)); + E.prefix = (struct prefix_t *)bAlloc(sizeof(struct prefix_t)); E.prefix[0].prefix_id = 0; strncpy(E.prefix[0].prefix_name, "no-prefix", 64); E.prefix_state = 0; - initConfig(); + E.lsp_client = (LspClient*)bAlloc(sizeof(LspClient)); + initConfig(); initTheme(); // To modify diff --git a/src/input.c b/src/input.c index b1c51d6..3473d65 100644 --- a/src/input.c +++ b/src/input.c @@ -6,6 +6,8 @@ #include "include/buffer.h" #include "include/data.h" #include "include/split_screen.h" +#include "include/completion.h" +#include "include/lsp_ui.h" #include #include #include @@ -18,6 +20,7 @@ #include "include/terminal.h" #include "include/utf8.h" +#include "include/utils.h" extern struct editorConfig E; @@ -37,7 +40,7 @@ extern struct editorConfig E; * - path ends with '/' (already a directory) * - no matching entries found * - directory cannot be opened - * @note Caller is responsible for freeing the returned string + * @note Caller is responsible for bFreeing the returned string * @note Uses static buffer internally; may return stale pointers across calls */ const char *fileCompletion(const char *path) { @@ -92,7 +95,7 @@ const char *fileCompletion(const char *path) { // Cleanup when no more entries closedir(dir); dir = NULL; - free(entry); + bFree(entry); appDebug("[FILE COMP] no entries\n"); return strdup(path); } @@ -108,13 +111,13 @@ const char *fileCompletion(const char *path) { * @return Pointer to the user-entered text (dynamically allocated), or NULL if: * - User pressed ESC to cancel * - Input buffer is empty when Enter is pressed - * @note Caller is responsible for freeing the returned string + * @note Caller is responsible for bFreeing the returned string * @note Uses editorReadKey() for input and editorRefreshScreen() for display */ char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) { size_t buf_size = 256; appDebug("[FILE COMP] %s %d\n", placeHolder, strlen(placeHolder)); - char *buf = malloc(buf_size); + char *buf = bAlloc(buf_size); size_t buf_len = 0; int c = 0; buf[0] = '\0'; @@ -131,7 +134,7 @@ char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) { } } else if (c == ESCAPE) { editorSetStatusMessage(""); - free(buf); + bFree(buf); return NULL; } else if (c == '\r') { if (buf_len != 0) { @@ -154,14 +157,14 @@ char *editorPrompt(char *prompt, char *placeHolder, char bPathMode) { buf_len = 0; char * buf_complete = (char *) fileCompletion(path); strcpy(buf, buf_complete); - free(buf_complete); + bFree(buf_complete); buf_len = strlen(buf); buf[buf_len] = '\0'; } else if (!iscntrl(c) && c < 256) { if (buf_len == buf_size - 1) { buf_size *= 2; - buf = realloc(buf, buf_size); + buf = bRealloc(buf, buf_size); } buf[buf_len++] = c; buf[buf_len] = '\0'; @@ -213,6 +216,52 @@ void editorProcessKeypress() { int c = editorReadKey(); char key_sequence[8]; + if (E.lsp_client && E.lsp_completion.visible) { + if (c == ARROW_UP || c == CTRL_KEY('p')) { + if (E.lsp_completion.selected > 0) + E.lsp_completion.selected--; + return; // consumed, redraw on next loop + } + if (c == ARROW_DOWN || c == CTRL_KEY('n')) { + if (E.lsp_completion.selected < E.lsp_completion.count - 1) + E.lsp_completion.selected++; + return; + } + if (c == '\r') { + CompletionItem *item = &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 + // current word (chars before cursor on the same line) + int file_col = active->cursor_x + active->x_offset; + row_t *row = &buf->row[active->cursor_y + active->y_offset]; + + // Walk backwards from cursor to find start of current word + int word_start = file_col; + while (word_start > 0 && + (isalnum((unsigned char)row->chars[word_start - 1]) || + row->chars[word_start - 1] == '_')) + word_start--; + + int already_typed = file_col - word_start; // chars user already typed + + // Insert only the suffix — what comes after what's already typed + const char *suffix = item->label + already_typed; + for (int i = 0; suffix[i]; i++) + bufferInsertBytes(&suffix[i], 1); + + E.lsp_completion.visible = 0; + return; + } + if (c == ESCAPE) { + E.lsp_completion.visible = 0; + return; + } + // Any other key: dismiss popup and fall through to normal handling + E.lsp_completion.visible = 0; + } + if (executeKeyBind(keyToString(c))) { return; } diff --git a/src/lsp_ui.c b/src/lsp_ui.c new file mode 100644 index 0000000..42ea1b2 --- /dev/null +++ b/src/lsp_ui.c @@ -0,0 +1,324 @@ +// +// Created by Giorgio on 27/05/2026. +// + +#include "include/lsp_ui.h" +#include "include/data.h" +#include +#include +#include + +// We use cJSON — drop cJSON.c + cJSON.h into src/ and include/ +#include "include/cJSON.h" +#include "include/append_buffer.h" + +// ─── ANSI helpers ───────────────────────────────────────────────────────────── + +// Move terminal cursor to absolute (col, row) — 1-based +static void ab_move(struct abuf *ab, int row, int col) { + char esc[32]; + int n = snprintf(esc, sizeof(esc), "\x1b[%d;%dH", row, col); + abAppend(ab, esc, n); // your existing abAppend +} + +static void ab_sgr(struct abuf *ab, const char *codes) { + char esc[32]; + int n = snprintf(esc, sizeof(esc), "\x1b[%sm", codes); + abAppend(ab, esc, n); +} + +#define AB_RESET(ab) ab_sgr(ab, "0") +#define AB_BOLD(ab) ab_sgr(ab, "1") +#define AB_DIM(ab) ab_sgr(ab, "2") +#define AB_FG(ab, c) ab_sgr(ab, c) // e.g. "32" = green +#define AB_BG(ab, c) ab_sgr(ab, c) // e.g. "44" = blue bg + +// ─── Completion item kind → short tag ──────────────────────────────────────── + +static const char *kind_tag(int k) { + switch (k) { + case 2: return "mth"; // Method + case 3: return "fn "; // Function + case 4: return "ctr"; // Constructor + case 5: return "fld"; // Field + case 6: return "var"; // Variable + case 7: return "cls"; // Class + case 9: return "ifc"; // Interface + case 14: return "kwd"; // Keyword + default: return " "; + } +} + +static const char *kind_color(int k) { + switch (k) { + case 3: return "33"; // yellow — functions + case 2: return "36"; // cyan — methods + case 7: return "35"; // magenta — classes + case 14: return "34"; // blue — keywords + case 5: + case 6: return "32"; // green — fields/vars + default: return "37"; // white + } +} + +// ─── Draw completion popup ─────────────────────────────────────────────────── +// +// ┌─────────────────────────────────────┐ +// │ fn printf int (const …) │ ← selected (bold, inverted) +// │ fn fprintf │ +// │ fn sprintf │ +// └─────────────────────────────────────┘ +// +// Drawn using absolute cursor positioning — safe to call inside +// editorRefreshScreen() before the final write(). + +void lspUiDrawCompletion(struct abuf *ab, CompletionPopup *popup) { + fprintf(stderr, "[DRAW] visible=%d count=%d origin=(%d,%d)\n", + popup->visible, popup->count, + popup->origin_x, popup->origin_y); + if (!popup->visible || popup->count == 0) return; + + int visible_count = popup->count < COMPLETION_POPUP_H + ? popup->count : COMPLETION_POPUP_H; + int box_w = COMPLETION_MAX_WIDTH; + + // Scroll window so selected item is always visible + int scroll = 0; + if (popup->selected >= COMPLETION_POPUP_H) + scroll = popup->selected - COMPLETION_POPUP_H + 1; + // ── top border ──────────────────────────────────────────────────────────── + ab_move(ab, popup->origin_y, popup->origin_x); + ab_sgr(ab, "0;37;40"); // white on black + abAppend(ab, "┌", 3); // UTF-8 box chars (3 bytes each) + for (int i = 0; i < box_w - 2; i++) abAppend(ab, "─", 3); + abAppend(ab, "┐", 3); + + // ── rows ────────────────────────────────────────────────────────────────── + for (int i = 0; i < visible_count; i++) { + int idx = i + scroll; + CompletionItem *it = &popup->items[idx]; + int is_sel = (idx == popup->selected); + + ab_move(ab, popup->origin_y + 1 + i, popup->origin_x); + + if (is_sel) ab_sgr(ab, "7"); // reverse video (highlight) + else ab_sgr(ab, "0;37;40"); + + abAppend(ab, "│ ", 2); + + // kind tag in color + if (!is_sel) ab_sgr(ab, kind_color(it->kind)); + abAppend(ab, kind_tag(it->kind), 3); + abAppend(ab, " ", 1); + if (!is_sel) AB_RESET(ab); + if (!is_sel) ab_sgr(ab, "37;40"); + + // label — left column, fixed width 20 + char label_col[21]; + snprintf(label_col, sizeof(label_col), "%-20s", it->label); + if (is_sel) AB_BOLD(ab); + abAppend(ab, label_col, 20); + if (is_sel) ab_sgr(ab, "22"); // bold off + + abAppend(ab, " ", 1); + + // detail — right column, dimmed + if (!is_sel) AB_DIM(ab); + char detail_col[14]; + snprintf(detail_col, sizeof(detail_col), "%-13s", it->detail); + abAppend(ab, detail_col, 13); + + AB_RESET(ab); + if (is_sel) ab_sgr(ab, "7"); + else ab_sgr(ab, "37;40"); + abAppend(ab, " │", 2); + AB_RESET(ab); + } + + // ── scroll indicator on right border if needed ──────────────────────────── + if (popup->count > COMPLETION_POPUP_H) { + int bar_row = popup->origin_y + 1 + + (popup->selected * (COMPLETION_POPUP_H - 1)) + / (popup->count - 1); + ab_move(ab, bar_row, popup->origin_x + box_w - 1); + ab_sgr(ab, "0;37;40"); + abAppend(ab, "█", 3); + AB_RESET(ab); + } + + // ── bottom border ───────────────────────────────────────────────────────── + ab_move(ab, popup->origin_y + 1 + visible_count, popup->origin_x); + ab_sgr(ab, "0;37;40"); + abAppend(ab, "└", 3); + for (int i = 0; i < box_w - 2; i++) abAppend(ab, "─", 3); + abAppend(ab, "┘", 3); + AB_RESET(ab); +} + +// ─── Handle popup keys ──────────────────────────────────────────────────────── +// Returns 1 if key was consumed, 0 if editor should handle it normally. + +int lspUiHandleKey(CompletionPopup *popup, int key) { + if (!popup->visible) return 0; + + switch (key) { + case '\x1b': // ESC — dismiss + popup->visible = 0; + return 1; + + case '\r': // Enter — accept + // Caller should insert popup->items[popup->selected].label + popup->visible = 0; + return 1; + + // Arrow up / Ctrl-p + case 'A' + 0x80: + case 0x10: + if (popup->selected > 0) popup->selected--; + return 1; + + // Arrow down / Ctrl-n + case 'B' + 0x80: + case 0x0e: + if (popup->selected < popup->count - 1) popup->selected++; + return 1; + + default: + // Any printable key: dismiss popup, let editor handle it + popup->visible = 0; + return 0; + } +} + +static DiagSeverity diag_worst_on_line(DiagnosticList *diags, + int buffer_id, int line) { + DiagSeverity worst = 0; + for (int i = 0; i < diags->count; i++) { + Diagnostic *d = &diags->entries[i]; + if (d->buffer_id == buffer_id && d->line == line) { + if (d->severity > worst) worst = d->severity; // ERROR > WARN > HINT + } + } + return worst; +} + +// Call this once per screen row, right before you draw the row text. +// ab : your append buffer +// diags : &E.lsp_diagnostics +// buf_id : active buffer id +// line : file line number (0-based) for this screen row +void lspUiDrawGutter(struct abuf *ab, DiagnosticList *diags, + int buf_id, int line) { + DiagSeverity s = diag_worst_on_line(diags, buf_id, line); + switch (s) { + case DIAG_ERROR: + ab_sgr(ab, "1;31"); // bold red + abAppend(ab, "●", 3); + break; + case DIAG_WARNING: + ab_sgr(ab, "1;33"); // bold yellow + abAppend(ab, "◆", 3); + break; + case DIAG_HINT: + ab_sgr(ab, "2;37"); // dim white + abAppend(ab, "·", 1); + break; + default: + abAppend(ab, " ", 1); // no diagnostic + break; + } + AB_RESET(ab); + abAppend(ab, " ", 1); // gutter padding +} + +// Returns the diagnostic message for the line the cursor is on, or NULL. +const char *lspUiDiagnosticAtCursor(DiagnosticList *diags, + int buffer_id, int cursor_line) { + for (int i = 0; i < diags->count; i++) { + Diagnostic *d = &diags->entries[i]; + if (d->buffer_id == buffer_id && d->line == cursor_line) + return d->message; + } + return NULL; +} + +// ─── Parse clangd JSON → our structs ───────────────────────────────────────── + +void lspParseCompletion(const char *json, CompletionPopup *popup, + int screen_x, int screen_y) { + popup->count = 0; + popup->selected = 0; + popup->origin_x = screen_x; + popup->origin_y = screen_y + 1; // one row below cursor + + cJSON *root = cJSON_Parse(json); + if (!root) return; + cJSON *result = cJSON_GetObjectItem(root, "result"); + if (!result) { cJSON_Delete(root); return; } + + // result can be a list or {isIncomplete, items:[…]} + cJSON *items = cJSON_IsArray(result) + ? result + : cJSON_GetObjectItem(result, "items"); + if (!items) { cJSON_Delete(root); return; } + + cJSON *item; + cJSON_ArrayForEach(item, items) { + if (popup->count >= COMPLETION_MAX_ITEMS) break; + CompletionItem *ci = &popup->items[popup->count++]; + + cJSON *label = cJSON_GetObjectItem(item, "label"); + cJSON *detail = cJSON_GetObjectItem(item, "detail"); + cJSON *kind = cJSON_GetObjectItem(item, "kind"); + + const char *raw_label = label ? label->valuestring : ""; + if (raw_label[0] == ' ') raw_label++; + + strncpy(ci->label, raw_label, 127); + strncpy(ci->detail, detail ? detail->valuestring : "", 63); + ci->kind = kind ? kind->valueint : 0; + } + + popup->visible = (popup->count > 0); + cJSON_Delete(root); +} + +void lspParseDiagnostics(const char *json, DiagnosticList *diags, + int buffer_id) { + cJSON *root = cJSON_Parse(json); + if (!root) return; + + cJSON *params = cJSON_GetObjectItem(root, "params"); + if (!params) { cJSON_Delete(root); return; } + cJSON *diag_arr = cJSON_GetObjectItem(params, "diagnostics"); + if (!diag_arr) { cJSON_Delete(root); return; } + + // Remove old entries for this buffer + int w = 0; + for (int i = 0; i < diags->count; i++) + if (diags->entries[i].buffer_id != buffer_id) + diags->entries[w++] = diags->entries[i]; + diags->count = w; + + cJSON *d; + cJSON_ArrayForEach(d, diag_arr) { + if (diags->count >= DIAG_MAX) break; + Diagnostic *diag = &diags->entries[diags->count++]; + diag->buffer_id = buffer_id; + + cJSON *range = cJSON_GetObjectItem(d, "range"); + cJSON *start = cJSON_GetObjectItem(range, "start"); + cJSON *end_pos = cJSON_GetObjectItem(range, "end"); + cJSON *sev = cJSON_GetObjectItem(d, "severity"); + cJSON *msg = cJSON_GetObjectItem(d, "message"); + + diag->line = cJSON_GetObjectItem(start, "line")->valueint; + diag->col_start = cJSON_GetObjectItem(start, "character")->valueint; + diag->col_end = cJSON_GetObjectItem(end_pos, "character")->valueint; + diag->severity = sev ? (DiagSeverity)sev->valueint : DIAG_ERROR; + + strncpy(diag->message, msg ? msg->valuestring : "", 255); + } + + cJSON_Delete(root); +} \ No newline at end of file diff --git a/src/output.c b/src/output.c index 41d87c6..fffb094 100644 --- a/src/output.c +++ b/src/output.c @@ -15,11 +15,15 @@ #include "../include/split_screen.h" #include "../include/syntax_highlighter.h" #include "../include/terminal.h" +#include "../include/lsp_ui.h" #include #include #include #include +#include "include/completion.h" +#include "include/utils.h" + extern struct editorConfig E; /** @@ -52,7 +56,7 @@ static void editorDrawPane(struct abuf* ab, EditorPane* pane) abAppend(ab, pos_buf, pos_len); // Apply background color (6 bytes for RGB format) - abAppend(ab, E.theme.BACKGROUND_COLOR, (int) strlen(E.theme.BACKGROUND_COLOR)); + abAppend(ab, E.theme.BACKGROUND_COLOR, (int)strlen(E.theme.BACKGROUND_COLOR)); // pane line is out of buffer @@ -70,7 +74,7 @@ static void editorDrawPane(struct abuf* ab, EditorPane* pane) } else { - + lspUiDrawGutter(ab, &E.lsp_diagnostics, pane->buffer_id, file_row); if (buf->filename[strlen(buf->filename) - 1] == 'c' || buf->filename[strlen(buf->filename) - 1] == 'h') { @@ -80,7 +84,7 @@ static void editorDrawPane(struct abuf* ab, EditorPane* pane) // Print only up to pane width abAppend(ab, highlighted, byte_len_to_print); - free(highlighted); + bFree(highlighted); } else { @@ -130,7 +134,7 @@ static void editorDrawAllPanes(struct abuf* ab) { char pos_buf[32]; snprintf(pos_buf, sizeof(pos_buf), "\x1b[%d;%dH", y + 1, divider_col); - abAppend(ab, pos_buf, (int) strlen(pos_buf)); + abAppend(ab, pos_buf, (int)strlen(pos_buf)); abAppend(ab, "\x1b[1m|\x1b[0m", 9); // Bold pipe divider } } @@ -140,7 +144,7 @@ static void editorDrawAllPanes(struct abuf* ab) int divider_row = layout->panes[0].height; char pos_buf[32]; snprintf(pos_buf, sizeof(pos_buf), "\x1b[%d;%dH", divider_row + 1, 1); - abAppend(ab, pos_buf, (int) strlen(pos_buf)); + abAppend(ab, pos_buf, (int)strlen(pos_buf)); for (int x = 0; x < E.screencols; x++) { abAppend(ab, "\x1b[1m-\x1b[0m", 9); // Bold dash divider @@ -180,11 +184,13 @@ void editorScroll() if (active->cursor_x == 0 && active->x_offset) { active->x_offset--; - } else + } + else { active->cursor_x--; } - } else + } + else { // RIGHT if (rel_x > active->cursor_x + active->x_offset) @@ -192,7 +198,8 @@ void editorScroll() if (active->cursor_x == active->width - 1) { active->x_offset++; - } else + } + else { active->cursor_x++; } @@ -204,7 +211,8 @@ void editorScroll() if (active->cursor_y == 0 && active->y_offset) { active->y_offset--; - } else + } + else { active->cursor_y--; } @@ -215,25 +223,25 @@ void editorScroll() if (active->cursor_y == active->height - 1) { active->y_offset++; - } else + } + else { active->cursor_y++; } } - } } -char * basename(char *path) +char* basename(char* path) { - int len = (int) strlen(path); + int len = (int)strlen(path); - for(int i=len-1; i>0; i--) + for (int i = len - 1; i > 0; i--) { - if(path[i]=='/' ) + if (path[i] == '/') { - path = path+i+1; + path = path + i + 1; break; } } @@ -316,7 +324,7 @@ void editorDrawStatusBar(struct abuf* ab) */ void editorDrawMessageBar(struct abuf* ab) { - int msg_len = (int) strlen(E.status_msg); + int msg_len = (int)strlen(E.status_msg); abAppend(ab, ERASE_END_LINE, 3); if (msg_len > E.screencols) { @@ -344,17 +352,17 @@ void editorDrawContextBuffer(struct abuf* ab) if (E.context_buffers->editor_y + i + 1 > 0) { pos_len = snprintf(pos_buf, sizeof(pos_buf), "\x1b[%d;%dH", - E.context_buffers->editor_y + i + 1, E.context_buffers->editor_x - 2); + E.context_buffers->editor_y + i + 1, E.context_buffers->editor_x - 2); abAppend(ab, pos_buf, pos_len); // Apply background color (6 bytes for RGB format) - abAppend(ab, E.theme.BACKGROUND_COLOR, (int) strlen(E.theme.BACKGROUND_COLOR)); + abAppend(ab, E.theme.BACKGROUND_COLOR, (int)strlen(E.theme.BACKGROUND_COLOR)); abAppend(ab, "|", 1); abAppend(ab, E.context_buffers->rows->chars, E.context_buffers->rows->size); abAppend(ab, "|", 1); } } - free(E.context_buffers); + bFree(E.context_buffers); E.context_buffers = NULL; } @@ -377,27 +385,55 @@ void editorRefreshScreen() abAppend(&ab, HIDE_CURSOR, 6); abAppend(&ab, CURSOR_TOP_LEFT, 3); abAppend(&ab, E.theme.BACKGROUND_COLOR, - (int) strlen(E.theme.BACKGROUND_COLOR)); // RGB background is 12 bytes + (int)strlen(E.theme.BACKGROUND_COLOR)); - // Draw all panes editorScroll(); editorDrawAllPanes(&ab); - - // Draw status bar and message bar editorDrawStatusBar(&ab); editorDrawMessageBar(&ab); - // editorDrawContextBuffer(&ab); - // Position cursor in active pane - EditorPane* active = splitScreenGetActivePane(); - if (active != NULL) - { - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", - active->cursor_y + active->origin_y + 1, - active->cursor_x + active->origin_x + 1); - abAppend(&ab, buf, (int) strlen(buf)); + EditorPane *active = splitScreenGetActivePane(); + struct buffer_t *buffer = bufferFindById(active->buffer_id); + + // ── LSP: sync buffer changes to clangd ──────────────────────────────────── + if (buffer->b_has_changed) { + if (E.lsp_client && E.lsp_client->state == LSP_READY) { + lspDidChange(E.lsp_client, buffer); + + E.lsp_client->completion_just_arrived = 0; // consume the flag + } + buffer->b_has_changed = 0; } + // ── LSP: draw completion popup every frame while visible ────────────────── + fprintf(stderr, "[REFRESH] lsp_completion.visible=%d\n", + E.lsp_completion.visible); + while (E.lsp_client->completion_requested && !E.lsp_client->completion_just_arrived) + ; + // reset flags + E.lsp_client->completion_just_arrived = 0; + E.lsp_client->completion_requested = 0; + + if (E.lsp_client && E.lsp_client->state == LSP_READY) + { + lspUiDrawCompletion(&ab, &E.lsp_completion); + appDebug("ready\n"); + } + + // ── LSP: diagnostic for current line in status bar ──────────────────────── + const char *diag = lspUiDiagnosticAtCursor( + &E.lsp_diagnostics, + active->buffer_id, + active->cursor_y + active->y_offset); + if (diag) + editorSetStatusMessage("● %s", diag); + + // ── Position cursor (account for gutter width) ──────────────────────────── + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", + active->cursor_y + active->origin_y + 1, + active->cursor_x + active->origin_x + 1 + GUTTER_WIDTH); + abAppend(&ab, buf, (int)strlen(buf)); + abAppend(&ab, SHOW_CURSOR, 6); write(STDOUT_FILENO, ab.b, ab.len); abFree(&ab); diff --git a/src/split_screen.c b/src/split_screen.c index 9fa11b0..5461b90 100644 --- a/src/split_screen.c +++ b/src/split_screen.c @@ -9,6 +9,8 @@ #include #include +#include "include/utils.h" + extern struct editorConfig E; /** @@ -19,7 +21,7 @@ void splitScreenInit(void) { E.layout.num_panes = 1; E.layout.active_pane = 0; - E.layout.panes = malloc(sizeof(EditorPane) * 2); + E.layout.panes = bAlloc(sizeof(EditorPane) * 2); // Initialize single fullscreen pane E.layout.panes[0].buffer_id = -1; // No buffer for now @@ -48,8 +50,8 @@ int splitScreenVertical(int buffer_id_left, int buffer_id_right) { return -1; } - // Reallocate panes array - E.layout.panes = realloc(E.layout.panes, sizeof(EditorPane) * 2); + // bReallocate panes array + E.layout.panes = bRealloc(E.layout.panes, sizeof(EditorPane) * 2); E.layout.mode = SPLIT_VERTICAL; E.layout.num_panes = 2; E.layout.active_pane = 0; @@ -99,8 +101,8 @@ int splitScreenHorizontal(int buffer_id_top, int buffer_id_bottom) { return -1; } - // Reallocate panes array - E.layout.panes = realloc(E.layout.panes, sizeof(EditorPane) * 2); + // bReallocate panes array + E.layout.panes = bRealloc(E.layout.panes, sizeof(EditorPane) * 2); E.layout.mode = SPLIT_HORIZONTAL; E.layout.num_panes = 2; E.layout.active_pane = 0; diff --git a/src/syntax_highlighter.c b/src/syntax_highlighter.c index ad0226c..ba178fe 100644 --- a/src/syntax_highlighter.c +++ b/src/syntax_highlighter.c @@ -5,6 +5,8 @@ #include #include +#include "include/utils.h" + extern struct editorConfig E; const char *c_keywords[] = { @@ -112,7 +114,7 @@ char *highlight_line(const char *line, int *length) { // Allocate generously based on line length to avoid overflow. int line_len = strlen(line); int buf_size = line_len * 32 + 256; - char *result = malloc(buf_size); + char *result = bAlloc(buf_size); int result_pos = 0; int i = 0; diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..bb45c50 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,38 @@ +// +// Created by Giorgio on 28/05/2026. +// + +#include "../include/utils.h" + +#include + + +int beluga_alloc_counter = 0; + +void * bAlloc(size_t size) +{ + void * result = malloc(size); + if (!result) + return NULL; + beluga_alloc_counter++; + return result; +} + +void * bRealloc(void * ptr, size_t size) +{ + void * result = realloc(ptr, size); + if (!result) + return NULL; + beluga_alloc_counter++; + return result; +} + +void * bFree(void * ptr) +{ + if (ptr) + { + free(ptr); + beluga_alloc_counter--; + } + return NULL; +} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..f5980c1 --- /dev/null +++ b/test/main.c @@ -0,0 +1,3 @@ +int main() { + printf("hello"); +}