A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/neovim/neovim/commit/e11a44aa224ae59670b992a73bfb029f77a75e76 below:

add vim.spell (#16620) · neovim/neovim@e11a44a · GitHub

File tree Expand file treeCollapse file tree 5 files changed

+201

-0

lines changed

Filter options

Expand file treeCollapse file tree 5 files changed

+201

-0

lines changed Original file line number Diff line number Diff line change

@@ -707,6 +707,38 @@ vim.mpack.encode({obj}) *vim.mpack.encode*

707 707

vim.mpack.decode({str}) *vim.mpack.decode*

708 708

Decodes (or "unpacks") the msgpack-encoded {str} to a Lua object.

709 709 710 +

------------------------------------------------------------------------------

711 +

VIM.SPELL *lua-spell*

712 + 713 +

vim.spell.check({str}) *vim.spell.check()*

714 +

Check {str} for spelling errors. Similar to the Vimscript function

715 +

|spellbadword()|.

716 + 717 +

Note: The behaviour of this function is dependent on: 'spelllang',

718 +

'spellfile', 'spellcapcheck' and 'spelloptions' which can all be local

719 +

to the buffer. Consider calling this with |nvim_buf_call()|.

720 + 721 +

Example: >

722 +

vim.spell.check("the quik brown fox")

723 +

-->

724 +

{

725 +

{'quik', 'bad', 4}

726 +

}

727 +

<

728 + 729 +

Parameters: ~

730 +

{str} String to spell check.

731 + 732 +

Return: ~

733 +

List of tuples with three items:

734 +

- The badly spelled word.

735 +

- The type of the spelling error:

736 +

"bad" spelling mistake

737 +

"rare" rare word

738 +

"local" word only valid in another region

739 +

"caps" word should start with Capital

740 +

- The position in {str} where the word begins.

741 + 710 742

------------------------------------------------------------------------------

711 743

VIM *lua-builtin*

712 744 Original file line number Diff line number Diff line change

@@ -0,0 +1,99 @@

1 + 2 +

#include <lua.h>

3 +

#include <lauxlib.h>

4 + 5 +

#include "nvim/spell.h"

6 +

#include "nvim/vim.h"

7 +

#include "nvim/lua/spell.h"

8 + 9 +

#ifdef INCLUDE_GENERATED_DECLARATIONS

10 +

# include "lua/spell.c.generated.h"

11 +

#endif

12 + 13 +

int nlua_spell_check(lua_State *lstate)

14 +

{

15 +

if (lua_gettop(lstate) < 1) {

16 +

return luaL_error(lstate, "Expected 1 argument");

17 +

}

18 + 19 +

if (lua_type(lstate, 1) != LUA_TSTRING) {

20 +

luaL_argerror(lstate, 1, "expected string");

21 +

}

22 + 23 +

const char *str = lua_tolstring(lstate, 1, NULL);

24 + 25 +

// spell.c requires that 'spell' is enabled, so we need to temporarily enable

26 +

// it before we can call spell functions.

27 +

const int wo_spell_save = curwin->w_p_spell;

28 + 29 +

if (!curwin->w_p_spell) {

30 +

did_set_spelllang(curwin);

31 +

curwin->w_p_spell = true;

32 +

}

33 + 34 +

// Check 'spelllang'

35 +

if (*curwin->w_s->b_p_spl == NUL) {

36 +

emsg(_(e_no_spell));

37 +

curwin->w_p_spell = wo_spell_save;

38 +

return 0;

39 +

}

40 + 41 +

hlf_T attr = HLF_COUNT;

42 +

size_t len = 0;

43 +

size_t pos = 0;

44 +

int capcol = -1;

45 +

int no_res = 0;

46 +

const char * result;

47 + 48 +

lua_createtable(lstate, 0, 0);

49 + 50 +

while (*str != NUL) {

51 +

attr = HLF_COUNT;

52 +

len = spell_check(curwin, (char_u *)str, &attr, &capcol, false);

53 +

assert(len <= INT_MAX);

54 + 55 +

if (attr != HLF_COUNT) {

56 +

lua_createtable(lstate, 3, 0);

57 + 58 +

lua_pushlstring(lstate, str, len);

59 +

lua_rawseti(lstate, -2, 1);

60 + 61 +

result = attr == HLF_SPB ? "bad" :

62 +

attr == HLF_SPR ? "rare" :

63 +

attr == HLF_SPL ? "local" :

64 +

attr == HLF_SPC ? "caps" :

65 +

NULL;

66 + 67 +

assert(result != NULL);

68 + 69 +

lua_pushstring(lstate, result);

70 +

lua_rawseti(lstate, -2, 2);

71 + 72 +

// +1 for 1-indexing

73 +

lua_pushinteger(lstate, (long)pos + 1);

74 +

lua_rawseti(lstate, -2, 3);

75 + 76 +

lua_rawseti(lstate, -2, ++no_res);

77 +

}

78 + 79 +

str += len;

80 +

pos += len;

81 +

capcol -= (int)len;

82 +

}

83 + 84 +

// Restore 'spell'

85 +

curwin->w_p_spell = wo_spell_save;

86 +

return 1;

87 +

}

88 + 89 +

static const luaL_Reg spell_functions[] = {

90 +

{ "check", nlua_spell_check },

91 +

{ NULL , NULL }

92 +

};

93 + 94 +

int luaopen_spell(lua_State *L)

95 +

{

96 +

lua_newtable(L);

97 +

luaL_register(L, NULL, spell_functions);

98 +

return 1;

99 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,12 @@

1 +

#ifndef NVIM_LUA_SPELL_H

2 +

#define NVIM_LUA_SPELL_H

3 + 4 +

#include <lauxlib.h>

5 +

#include <lua.h>

6 +

#include <lualib.h>

7 + 8 +

#ifdef INCLUDE_GENERATED_DECLARATIONS

9 +

# include "lua/spell.h.generated.h"

10 +

#endif

11 + 12 +

#endif // NVIM_LUA_SPELL_H

Original file line number Diff line number Diff line change

@@ -30,6 +30,7 @@

30 30

#include "nvim/lua/stdlib.h"

31 31

#include "nvim/lua/treesitter.h"

32 32

#include "nvim/lua/xdiff.h"

33 +

#include "nvim/lua/spell.h"

33 34

#include "nvim/macros.h"

34 35

#include "nvim/map.h"

35 36

#include "nvim/memline.h"

@@ -518,6 +519,10 @@ void nlua_state_add_stdlib(lua_State *const lstate)

518 519

lua_pushcfunction(lstate, &nlua_xdl_diff);

519 520

lua_setfield(lstate, -2, "diff");

520 521 522 +

// vim.spell

523 +

luaopen_spell(lstate);

524 +

lua_setfield(lstate, -2, "spell");

525 + 521 526

lua_cjson_new(lstate);

522 527

lua_setfield(lstate, -2, "json");

523 528

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,53 @@

1 +

local helpers = require('test.functional.helpers')(after_each)

2 +

local clear = helpers.clear

3 +

local exec_lua = helpers.exec_lua

4 +

local eq = helpers.eq

5 +

local pcall_err = helpers.pcall_err

6 + 7 +

describe('vim.spell', function()

8 +

before_each(function()

9 +

clear()

10 +

end)

11 + 12 +

describe('.check', function()

13 +

local check = function(x, exp)

14 +

return eq(exp, exec_lua("return vim.spell.check(...)", x))

15 +

end

16 + 17 +

it('can handle nil', function()

18 +

eq([[Error executing lua: [string "<nvim>"]:0: bad argument #1 to 'check' (expected string)]],

19 +

pcall_err(exec_lua, [[vim.spell.check(nil)]]))

20 +

end)

21 + 22 +

it('can check spellings', function()

23 +

check('hello', {})

24 + 25 +

check(

26 +

'helloi',

27 +

{{"helloi", "bad", 1}}

28 +

)

29 + 30 +

check(

31 +

'hello therei',

32 +

{{"therei", "bad", 7}}

33 +

)

34 + 35 +

check(

36 +

'hello. there',

37 +

{{"there", "caps", 8}}

38 +

)

39 + 40 +

check(

41 +

'neovim cna chkc spellins. okay?',

42 +

{

43 +

{"neovim" , "bad" , 1},

44 +

{"cna" , "bad" , 8},

45 +

{"chkc" , "bad" , 12},

46 +

{"spellins", "bad" , 17},

47 +

{"okay" , "caps", 27}

48 +

}

49 +

)

50 +

end)

51 + 52 +

end)

53 +

end)

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4