Skip to content

Commit b9c3a9a

Browse files
Copilotkwerle
andcommitted
Improve namespace reference detection logic
- Use exclusive range for line_before_cursor (characters before cursor) - Check pattern position relative to cursor more precisely - Handle clicking anywhere in the namespaced class name - Prevent false positives when pattern appears elsewhere on line Co-authored-by: kwerle <[email protected]>
1 parent b6c20b9 commit b9c3a9a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/ruby_language_server/project_manager.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,18 @@ def namespace_reference?(uri, position, context)
244244
line = lines[position.line]
245245
return false if line.nil?
246246

247-
# Get the substring up to the cursor position
248-
line_up_to_cursor = line[0..position.character]
249-
250-
# Check if the line contains :: before the cursor position
251-
# and all context parts appear with :: separators
247+
# Build the expected pattern with :: separators
252248
context_pattern = context.join('::')
253-
line_up_to_cursor.include?('::') && line_up_to_cursor.include?(context_pattern)
249+
250+
# LineContext extracts the full word under the cursor and works backward.
251+
# We need to check if the pattern with :: separators appears in the line
252+
# at a position that could reasonably contain the cursor.
253+
# Look for the pattern starting before or at the cursor position.
254+
pattern_start = line.rindex(context_pattern, position.character + context.last.length)
255+
256+
# If we found the pattern and it's near the cursor position, it's a namespace reference
257+
!pattern_start.nil? && pattern_start <= position.character &&
258+
(position.character <= pattern_start + context_pattern.length)
254259
end
255260

256261
# Guess if a receiver name is likely a class name based on idiomatic Ruby conventions.

0 commit comments

Comments
 (0)