From 021866de868fd074526fc63270639ec2f3ce9aa5 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Sat, 4 Jul 2020 23:22:03 -0400 Subject: Added `buffer:style_of_name()` as an analogue to `buffer:name_of_style()`. --- core/.buffer.luadoc | 9 +++++++++ core/init.lua | 12 +++++++++++- modules/textadept/run.lua | 6 +++--- test/test.lua | 19 ++++++++++++++++--- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/core/.buffer.luadoc b/core/.buffer.luadoc index 810075fd..175e1098 100644 --- a/core/.buffer.luadoc +++ b/core/.buffer.luadoc @@ -1782,6 +1782,15 @@ function new() end -- @param end_pos The end position of the range of text to get in *buffer*. function text_range(buffer, start_pos, end_pos) end +--- +-- Returns the style number associated with string *style_name*, or +-- `view.STYLE_DEFAULT` if *style_name* is not in use. +-- @param buffer A buffer. +-- @param string The style name to get the number of. +-- @return style number, between `1` and `256`. +-- @see name_of_style +function style_of_name(buffer, style_name) end + --- -- Reloads the buffer's file contents, discarding any changes. -- @param buffer A buffer. diff --git a/core/init.lua b/core/init.lua index 1768b1e2..07c9e060 100644 --- a/core/init.lua +++ b/core/init.lua @@ -54,7 +54,17 @@ local function text_range(buffer, start_pos, end_pos) buffer:set_target_range(target_start, target_end) -- restore return text end -events.connect(events.BUFFER_NEW, function() buffer.text_range = text_range end) + +-- Documentation is in core/.buffer.luadoc. +local function style_of_name(buffer, style_name) + assert_type(style_name, 'string', 2) + local GETNAMEDSTYLE = _SCINTILLA.properties.named_styles[1] + return buffer:private_lexer_call(GETNAMEDSTYLE, style_name) +end + +events.connect(events.BUFFER_NEW, function() + buffer.text_range, buffer.style_of_name = text_range, style_of_name +end) --[[ This comment is for LuaDoc. --- diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index 2694166f..561d873a 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -413,9 +413,9 @@ function M.goto_error(line_num, next) end if detail.message then buffer.annotation_text[detail.line] = detail.message - local GETNAMEDSTYLE = _SCINTILLA.properties.named_styles[1] - local style = buffer:private_lexer_call(GETNAMEDSTYLE, 'error') - if not detail.warning then buffer.annotation_style[detail.line] = style end + if not detail.warning then + buffer.annotation_style[detail.line] = buffer:style_of_name('error') + end end end events.connect(events.KEYPRESS, function(code) diff --git a/test/test.lua b/test/test.lua index 08881604..33e8fd67 100644 --- a/test/test.lua +++ b/test/test.lua @@ -2958,14 +2958,13 @@ end function test_view_split_refresh_styles() io.open_file(_HOME .. '/init.lua') - local GETNAMEDSTYLE = _SCINTILLA.properties.named_styles[1] - local style = buffer:private_lexer_call(GETNAMEDSTYLE, 'library') + local style = buffer:style_of_name('library') assert(style > 1, 'cannot retrieve number of library style') local color = view.style_fore[style] assert(color ~= view.style_fore[view.STYLE_DEFAULT], 'library style not set') view:split() for _, view in ipairs(_VIEWS) do - local view_style = buffer:private_lexer_call(GETNAMEDSTYLE, 'library') + local view_style = buffer:style_of_name('library') assert_equal(view_style, style) local view_color = view.style_fore[view_style] assert_equal(view_color, color) @@ -2998,7 +2997,21 @@ function test_set_theme() end function test_set_lexer_style() + buffer.new() + buffer:set_lexer('java') + buffer:add_text('foo()') + buffer:colorize(1, -1) + local style = buffer:style_of_name('function') + assert_equal(buffer.style_at[1], style) + local default_fore = view.style_fore[view.STYLE_DEFAULT] + assert(view.style_fore[style] ~= default_fore, 'function name style_fore same as default style_fore') + view.style_fore[style] = view.style_fore[view.STYLE_DEFAULT] + assert_equal(buffer.style_fore[style], default_fore) + buffer:close(true) -- Defined in Lua lexer, which is not currently loaded. + assert(buffer:style_of_name('library'), view.STYLE_DEFAULT) + -- Emulate a theme setting to trigger an LPeg lexer style refresh, but without + -- a token defined. view.property['style.library'] = view.property['style.library'] end -- cgit v1.2.3