Skip to content

Don't create tokens by hand#1696

Open
Earlopain wants to merge 1 commit intoruby:masterfrom
Earlopain:no-manual-token-creation
Open

Don't create tokens by hand#1696
Earlopain wants to merge 1 commit intoruby:masterfrom
Earlopain:no-manual-token-creation

Conversation

@Earlopain
Copy link
Copy Markdown
Contributor

There is some awkward code that dances around the fact that the tokens for a method actually contain a 3 extra tokens that don't exist in the source code.

Now RipperStateLex is only referenced to actually parse, rest is kept internal

Copilot AI review requested due to automatic review settings May 6, 2026 07:02
@Earlopain Earlopain deployed to fork-preview-protection May 6, 2026 07:02 — with GitHub Actions Active

def test_markup_code
tokens = [
{ :line_no => 0, :char_no => 0, :kind => :on_const, :text => 'CONSTANT' },
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testcase didn't make much sense since only methods have their source code shown. I simply removed it.

@matzbot
Copy link
Copy Markdown
Collaborator

matzbot commented May 6, 2026

🚀 Preview deployment available at: https://baaa25b1.rdoc-6cd.pages.dev (commit: 45fa797)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes “synthetic” tokens (file/line comment, newline, indent) from Ruby parser token streams and moves location/line-number decoration into RDoc::MethodAttr#markup_code, with tests updated to build token streams via RipperStateLex instead of hand-crafted hashes.

Changes:

  • Stop prepending non-source tokens in RDoc::Parser::Ruby#visible_tokens_from_location and related paths.
  • Rework RDoc::MethodAttr#markup_code to dedent, optionally add line numbers, and prepend a generated location comment.
  • Update any_method_test to use RipperStateLex.parse and assert the new HTML output format.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
test/rdoc/code_object/any_method_test.rb Updates expectations and replaces hand-built token hashes with RipperStateLex.parse output.
lib/rdoc/parser/ruby.rb Removes construction of extra non-source tokens from token streams; returns only sliced source tokens.
lib/rdoc/generator/markup.rb Moves location comment and line-number generation into markup_code, and adjusts dedent logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/rdoc/generator/markup.rb
Comment thread lib/rdoc/generator/markup.rb
Comment thread lib/rdoc/generator/markup.rb Outdated
Comment thread lib/rdoc/generator/markup.rb Outdated
Comment thread lib/rdoc/parser/ruby.rb
Comment on lines 315 to 319
meth.start_collecting_tokens(:ruby)
node = @line_nodes[line_no]
tokens = node ? visible_tokens_from_location(node.location) : [file_line_comment_token(start_line)]
tokens = node ? visible_tokens_from_location(node.location) : []
tokens.each { |token| meth.token_stream << token }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct. Previously they were not empty because it contained the comment

Comment thread lib/rdoc/parser/ruby.rb
line_no = node.location.start_line
else
tokens = [file_line_comment_token(line_no)]
tokens = []
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

Comment thread test/rdoc/code_object/any_method_test.rb Outdated
Comment thread lib/rdoc/generator/markup.rb
There is some awkward code that dances around the fact
that the tokens for a method actually contain a 3 extra tokens
that don't exist in the source code.

Now `RipperStateLex` is only referenced to actually parse, rest is kept internal
@Earlopain Earlopain force-pushed the no-manual-token-creation branch from 45fa797 to 187755a Compare May 6, 2026 08:57
@Earlopain Earlopain requested a deployment to fork-preview-protection May 6, 2026 08:57 — with GitHub Actions Waiting
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.

3 participants