@@ -699,24 +699,87 @@ impl Backend {
699699 pub fn find_definition_position ( content : & str , class_name : & str ) -> Option < Position > {
700700 let keywords = [ "class" , "interface" , "trait" , "enum" ] ;
701701
702+ // Track whether we are inside a `/* … */` block comment.
703+ let mut in_block_comment = false ;
704+
702705 for ( line_idx, line) in content. lines ( ) . enumerate ( ) {
706+ // ── Block-comment tracking ──────────────────────────────────
707+ // Walk through the line handling `/*` and `*/` toggles so we
708+ // know whether the keyword match is inside a comment.
709+ let mut effective_line = String :: new ( ) ;
710+ let line_bytes = line. as_bytes ( ) ;
711+ let mut i = 0 ;
712+ while i < line_bytes. len ( ) {
713+ if in_block_comment {
714+ // Look for closing `*/`.
715+ if i + 1 < line_bytes. len ( )
716+ && line_bytes[ i] == b'*'
717+ && line_bytes[ i + 1 ] == b'/'
718+ {
719+ in_block_comment = false ;
720+ // Replace the `*/` with spaces to preserve column offsets.
721+ effective_line. push ( ' ' ) ;
722+ effective_line. push ( ' ' ) ;
723+ i += 2 ;
724+ } else {
725+ effective_line. push ( ' ' ) ;
726+ i += 1 ;
727+ }
728+ } else if i + 1 < line_bytes. len ( )
729+ && line_bytes[ i] == b'/'
730+ && line_bytes[ i + 1 ] == b'*'
731+ {
732+ // Opening `/*` — rest of line (until `*/`) is a comment.
733+ in_block_comment = true ;
734+ effective_line. push ( ' ' ) ;
735+ effective_line. push ( ' ' ) ;
736+ i += 2 ;
737+ } else if i + 1 < line_bytes. len ( )
738+ && line_bytes[ i] == b'/'
739+ && line_bytes[ i + 1 ] == b'/'
740+ {
741+ // Line comment `//` — blank out the rest of the line.
742+ while i < line_bytes. len ( ) {
743+ effective_line. push ( ' ' ) ;
744+ i += 1 ;
745+ }
746+ } else if line_bytes[ i] == b'#' {
747+ // Line comment `#` — blank out the rest of the line.
748+ while i < line_bytes. len ( ) {
749+ effective_line. push ( ' ' ) ;
750+ i += 1 ;
751+ }
752+ } else {
753+ effective_line. push ( line_bytes[ i] as char ) ;
754+ i += 1 ;
755+ }
756+ }
757+
703758 for keyword in & keywords {
704759 // Search for `keyword ClassName` making sure ClassName is
705760 // followed by a word boundary (whitespace, `{`, `:`, end of
706761 // line) so we don't match partial names.
707762 let pattern = format ! ( "{} {}" , keyword, class_name) ;
708- if let Some ( col) = line . find ( & pattern) {
763+ if let Some ( col) = effective_line . find ( & pattern) {
709764 // Verify word boundary before the keyword: either start
710765 // of line or preceded by whitespace / non-alphanumeric.
711766 let before_ok = col == 0 || {
712- let prev = line. as_bytes ( ) . get ( col - 1 ) . copied ( ) . unwrap_or ( b' ' ) ;
767+ let prev = effective_line
768+ . as_bytes ( )
769+ . get ( col - 1 )
770+ . copied ( )
771+ . unwrap_or ( b' ' ) ;
713772 !( prev as char ) . is_alphanumeric ( ) && prev != b'_'
714773 } ;
715774
716775 // Verify word boundary after the class name.
717776 let after_pos = col + pattern. len ( ) ;
718- let after_ok = after_pos >= line. len ( ) || {
719- let next = line. as_bytes ( ) . get ( after_pos) . copied ( ) . unwrap_or ( b' ' ) ;
777+ let after_ok = after_pos >= effective_line. len ( ) || {
778+ let next = effective_line
779+ . as_bytes ( )
780+ . get ( after_pos)
781+ . copied ( )
782+ . unwrap_or ( b' ' ) ;
720783 !( next as char ) . is_alphanumeric ( ) && next != b'_'
721784 } ;
722785
0 commit comments