From 0ecfd7bebcc872955773be8280b3f222b8a705ed Mon Sep 17 00:00:00 2001 From: Luke <11898833+curlpipe@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:45:47 +0100 Subject: [PATCH] extended API and added syntax sugar for defining syntax highlighting --- .todo.md | 31 ++++++++++++++++++------------- config/.oxrc | 10 ++++++++-- src/config.rs | 11 +++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.todo.md b/.todo.md index 8479125..b0fdf45 100644 --- a/.todo.md +++ b/.todo.md @@ -1,14 +1,13 @@ - [ ] Plugin refresh - 0.4.2 - - [ ] Custom syntax highlighting rules - - [ ] Plugin management - - [ ] Expanded editor API - - [ ] Update documentation - - [X] Fields - - [X] visibility of help message - - [X] ox version - - [X] number of documents open - - [X] current document id - - [X] file type + - [ ] Investigate panics in plug-ins + - [ ] Update documentation + - [ ] New API + - [ ] User defined syntax highlighting + - [X] README update + - [X] Fix string error + - [X] Transfer built-in commands to lua & fix borrowmut panic + - [X] Custom syntax highlighting + - [X] Expanded editor API - [X] Methods - [X] Get character - [X] Get line @@ -21,9 +20,14 @@ - [X] Hide the help message - [X] Set file type - [X] Set read only status - - [X] README update - - [X] Fix string error - - [X] Transfer built-in commands to lua & fix borrowmut panic + - [X] Fields + - [X] visibility of help message + - [X] ox version + - [X] number of documents open + - [X] current document id + - [X] file type + - [X] length of current document + - [X] Custom syntax highlighting syntax sugar - [ ] Editing improvements - 0.4.3 - [ ] Better editor navigation (see github issues) @@ -31,6 +35,7 @@ - [ ] Bracket and Quote Pairs - [ ] Auto indentation - [ ] Delete word command + - [ ] Add documentation on how to use/see/import/package plugins - [ ] UI/UX improvements - 0.4.4 - [ ] Replace command needs to update syntax diff --git a/config/.oxrc b/config/.oxrc index eb43271..6252cc6 100644 --- a/config/.oxrc +++ b/config/.oxrc @@ -85,7 +85,7 @@ event_mapping = { editor:open_command_line() end, ["ctrl_h"] = function() - editor:set_read_only(false) + editor:display_info(editor.document_length) end, } @@ -218,7 +218,13 @@ syntax:new( -- New syntax highlighting for files with the extensions psi and psix {"psi", "psix"}, { - syntax:keyword("keyword", "pub"), + -- Add multiple patterns to make a keyword token + syntax:keywords("keyword", { + "\\b(pub)\\b", "\\b(fn)\\b", "\\b(return)\\b", + }), + -- Define digits + syntax:keyword("digit", "([0-9]+)"), + -- Create a bounded token for strings (that can be escaped) syntax:bounded("string", "\"", "\"", true), } ) diff --git a/src/config.rs b/src/config.rs index 1f170d7..75be51f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,6 +111,13 @@ impl SyntaxHighlighting { impl LuaUserData for SyntaxHighlighting { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method_mut("keywords", |lua, _, (name, pattern): (String, Vec)| { + let table = lua.create_table()?; + table.set("kind", "keyword")?; + table.set("name", name)?; + table.set("pattern", format!("({})", pattern.join("|")))?; + Ok(table) + }); methods.add_method_mut("keyword", |lua, _, (name, pattern): (String, String)| { let table = lua.create_table()?; table.set("kind", "keyword")?; @@ -725,6 +732,10 @@ impl LuaUserData for Editor { let name = editor.doc().file_name.clone(); Ok(name) }); + fields.add_field_method_get("document_length", |_, editor| { + let len = editor.doc().len_lines(); + Ok(len) + }); fields.add_field_method_get("version", |_, _| { Ok(VERSION) });