A RetroSearch Logo

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

Search Query:

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

allow Atom-style capture fallbacks (#14196) · neovim/neovim@8ab5ec4 · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+104

-3

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+104

-3

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

@@ -22,7 +22,21 @@ local _link_default_highlight_once = function(from, to)

22 22

return from

23 23

end

24 24 25 -

TSHighlighter.hl_map = {

25 +

-- If @definition.special does not exist use @definition instead

26 +

local subcapture_fallback = {

27 +

__index = function(self, capture)

28 +

local rtn

29 +

local shortened = capture

30 +

while not rtn and shortened do

31 +

shortened = shortened:match('(.*)%.')

32 +

rtn = shortened and rawget(self, shortened)

33 +

end

34 +

rawset(self, capture, rtn or "__notfound")

35 +

return rtn

36 +

end

37 +

}

38 + 39 +

TSHighlighter.hl_map = setmetatable({

26 40

["error"] = "Error",

27 41 28 42

-- Miscs

@@ -66,7 +80,7 @@ TSHighlighter.hl_map = {

66 80

["type.builtin"] = "Type",

67 81

["structure"] = "Structure",

68 82

["include"] = "Include",

69 -

}

83 +

}, subcapture_fallback)

70 84 71 85

---@private

72 86

local function is_highlight_name(capture_name)

Original file line number Diff line number Diff line change

@@ -23,7 +23,7 @@ local hl_query = [[

23 23

"enum" @type

24 24

"extern" @type

25 25 26 -

(string_literal) @string

26 +

(string_literal) @string.nonexistent-specializer-for-string.should-fallback-to-string

27 27 28 28

(number_literal) @number

29 29

(char_literal) @string

@@ -613,4 +613,91 @@ describe('treesitter highlighting', function()

613 613

[12] = {background = Screen.colors.Red, bold = true, foreground = Screen.colors.Grey100};

614 614

}}

615 615

end)

616 + 617 +

it("allows to use captures with dots (don't use fallback when specialization of foo exists)", function()

618 +

if pending_c_parser(pending) then return end

619 + 620 +

insert([[

621 +

char* x = "Will somebody ever read this?";

622 +

]])

623 + 624 +

screen:expect{grid=[[

625 +

char* x = "Will somebody ever read this?"; |

626 +

^ |

627 +

{1:~ }|

628 +

{1:~ }|

629 +

{1:~ }|

630 +

{1:~ }|

631 +

{1:~ }|

632 +

{1:~ }|

633 +

{1:~ }|

634 +

{1:~ }|

635 +

{1:~ }|

636 +

{1:~ }|

637 +

{1:~ }|

638 +

{1:~ }|

639 +

{1:~ }|

640 +

{1:~ }|

641 +

{1:~ }|

642 +

|

643 +

]]}

644 + 645 +

exec_lua [[

646 +

local parser = vim.treesitter.get_parser(0, "c", {})

647 +

local highlighter = vim.treesitter.highlighter

648 +

highlighter.hl_map['foo.bar'] = 'Type'

649 +

highlighter.hl_map['foo'] = 'String'

650 +

test_hl = highlighter.new(parser, {queries = {c = "(primitive_type) @foo.bar (string_literal) @foo"}})

651 +

]]

652 + 653 +

screen:expect{grid=[[

654 +

{3:char}* x = {5:"Will somebody ever read this?"}; |

655 +

^ |

656 +

{1:~ }|

657 +

{1:~ }|

658 +

{1:~ }|

659 +

{1:~ }|

660 +

{1:~ }|

661 +

{1:~ }|

662 +

{1:~ }|

663 +

{1:~ }|

664 +

{1:~ }|

665 +

{1:~ }|

666 +

{1:~ }|

667 +

{1:~ }|

668 +

{1:~ }|

669 +

{1:~ }|

670 +

{1:~ }|

671 +

|

672 +

]]}

673 +

end)

674 + 675 +

it("hl_map has the correct fallback behavior", function()

676 +

exec_lua [[

677 +

local hl_map = vim.treesitter.highlighter.hl_map

678 +

hl_map["foo"] = 1

679 +

hl_map["foo.bar"] = 2

680 +

hl_map["foo.bar.baz"] = 3

681 + 682 +

assert(hl_map["foo"] == 1)

683 +

assert(hl_map["foo.a.b.c.d"] == 1)

684 +

assert(hl_map["foo.bar"] == 2)

685 +

assert(hl_map["foo.bar.a.b.c.d"] == 2)

686 +

assert(hl_map["foo.bar.baz"] == 3)

687 +

assert(hl_map["foo.bar.baz.d"] == 3)

688 + 689 +

hl_map["FOO"] = 1

690 +

hl_map["FOO.BAR"] = 2

691 +

assert(hl_map["FOO.BAR.BAZ"] == 2)

692 + 693 +

hl_map["foo.missing.exists"] = 3

694 +

assert(hl_map["foo.missing"] == 1)

695 +

assert(hl_map["foo.missing.exists"] == 3)

696 +

assert(hl_map["foo.missing.exists.bar"] == 3)

697 +

assert(hl_map["total.nonsense.but.a.lot.of.dots"] == nil)

698 +

-- It will not perform a second look up of this variable but return a sentinel value

699 +

assert(hl_map["total.nonsense.but.a.lot.of.dots"] == "__notfound")

700 +

]]

701 + 702 +

end)

616 703

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