Skip to content

Commit b179d7d

Browse files
authored
Merge pull request #84 from r7kamura/feature/modifier
Fix highlight bug on modifier `if`, `unless`, `while`, and `until`
2 parents 2bd3ef5 + e743b87 commit b179d7d

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

lib/rucoa/handlers/text_document_document_highlight_handler.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class IfMapper < Base
175175
# @return [Array<Parser::Source::Range>]
176176
def call
177177
return AnyMapper.call(@node.parent) if @node.elsif?
178+
return [] if @node.modifier?
178179

179180
[
180181
@node.location.keyword,
@@ -244,6 +245,8 @@ def call
244245
class WhileMapper < Base
245246
# @return [Array<Parser::Source::Range>]
246247
def call
248+
return [] if @node.modifier?
249+
247250
[
248251
@node.location.keyword,
249252
@node.location.end

lib/rucoa/node_concerns.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Rucoa
44
module NodeConcerns
55
autoload :Body, 'rucoa/node_concerns/body'
6+
autoload :Modifier, 'rucoa/node_concerns/modifier'
67
autoload :QualifiedName, 'rucoa/node_concerns/qualified_name'
78
autoload :Rescue, 'rucoa/node_concerns/rescue'
89
end

lib/rucoa/node_concerns/modifier.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Rucoa
4+
module NodeConcerns
5+
module Modifier
6+
# @return [Boolean]
7+
# @example returns true on modifier if node
8+
# node = Rucoa::Source.new(
9+
# content: <<~RUBY,
10+
# 1 if true
11+
# RUBY
12+
# uri: 'file:///path/to/example.rb'
13+
# ).node_at(
14+
# Rucoa::Position.new(
15+
# column: 2,
16+
# line: 1
17+
# )
18+
# )
19+
# expect(node).to be_modifier
20+
# @example returns false on non-modifier if node
21+
# node = Rucoa::Source.new(
22+
# content: <<~RUBY,
23+
# if true
24+
# 1
25+
# end
26+
# RUBY
27+
# uri: 'file:///path/to/example.rb'
28+
# ).root_node
29+
# expect(node).not_to be_modifier
30+
def modifier?
31+
location.end.nil?
32+
end
33+
end
34+
end
35+
end

lib/rucoa/nodes/if_node.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Rucoa
44
module Nodes
55
class IfNode < Base
6+
include NodeConcerns::Modifier
7+
68
# @return [Rucoa::Nodes::Base, nil]
79
def branch_else
810
children[2]

lib/rucoa/nodes/until_node.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Rucoa
44
module Nodes
55
class UntilNode < Base
6+
include NodeConcerns::Modifier
67
end
78
end
89
end

lib/rucoa/nodes/while_node.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Rucoa
44
module Nodes
55
class WhileNode < Base
6+
include NodeConcerns::Modifier
67
end
78
end
89
end

spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,5 +720,59 @@ def foo
720720
)
721721
end
722722
end
723+
724+
context 'when one-lined if is detected' do
725+
let(:content) do
726+
<<~RUBY
727+
1 if foo
728+
RUBY
729+
end
730+
731+
let(:position) do
732+
Rucoa::Position.new(
733+
column: 2,
734+
line: 1
735+
)
736+
end
737+
738+
it 'returns empty' do
739+
subject
740+
expect(server.responses).to match(
741+
[
742+
hash_including(
743+
'id' => 1,
744+
'result' => []
745+
)
746+
]
747+
)
748+
end
749+
end
750+
751+
context 'when one-lined while is detected' do
752+
let(:content) do
753+
<<~RUBY
754+
1 while true
755+
RUBY
756+
end
757+
758+
let(:position) do
759+
Rucoa::Position.new(
760+
column: 2,
761+
line: 1
762+
)
763+
end
764+
765+
it 'returns empty' do
766+
subject
767+
expect(server.responses).to match(
768+
[
769+
hash_including(
770+
'id' => 1,
771+
'result' => []
772+
)
773+
]
774+
)
775+
end
776+
end
723777
end
724778
end

0 commit comments

Comments
 (0)