Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

which-function returns the enclosing class/module name when cursor is on a TODO comment between the class definition and a method definition, causing ai-code-implement-todo to report incorrect context.

module Foo
  class Bar
    # TODO remove this function  # <-- Cursor here
    def click_first_available(driver, selectors)
      # ...
    end
  end
end

Before: detects Bar (class)
After: detects Bar#click_first_available (method)

Changes

  • New helper ai-code--get-function-name-for-comment: When cursor is on a comment line, looks ahead to the next non-comment code line and returns the more specific function context. Handles common delimiters (., #, ::) and edge cases (nil values, EOF, multiple comment lines).

  • Updated ai-code-implement-todo: Calls helper when is-comment is true instead of directly calling which-function.

  • Tests: Added test_ai-code-change.el covering basic detection, edge cases, and multi-language comment syntax.

Language-agnostic and backward-compatible—only affects behavior when cursor is on a comment line.

Original prompt

This section details on the original issue you should resolve

<issue_title>Wrong function detection</issue_title>
<issue_description>Hello,

In the following example:

module Foo
  class Bar
    def baz
    end

    # TODO remove this function and don't use wait_until. When several selectors are passed just use the most sensible one
    def click_first_available(driver, selectors)
      wait = Selenium::WebDriver::Wait.new(timeout: 10)

      Array(selectors).each do |selector|
        strategy, locator = selector.split(':', 2)
        next if locator.nil?

        begin
          element = wait.until { driver.find_element(strategy.to_sym, locator) }
          element.click
          return
        rescue Selenium::WebDriver::Error::NoSuchElementError,
               Selenium::WebDriver::Error::TimeoutError,
               Selenium::WebDriver::Error::ElementClickInterceptedError
          next
        end
      end
    end
  end
end

It uncorrectly generates the text to mention function Bar#baz instead of Bar#click_first_availale.</issue_description>

Comments on the Issue (you are @copilot in this section)

@tninja ai-code-implement-todo use which-function to identify the function context. so the TODO place do have (which-function) == Bar. If the TODO is inside the function body of which-function, it should work as expected

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 7, 2025 05:29
Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
…comments

Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove click_first_available function from Bar class Fix function detection for TODO comments preceding method definitions Nov 7, 2025
Copilot AI requested a review from tninja November 7, 2025 05:37
Copilot finished work on behalf of tninja November 7, 2025 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong function detection

2 participants