Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of long strings and symbols in highlight() #21

Merged
merged 3 commits into from
Nov 10, 2022

Conversation

moodymudskipper
Copy link
Contributor

@moodymudskipper moodymudskipper commented Nov 10, 2022

Closes #20

We implement get_parse_data() as an alternative to getParseDate(, includeText = NA)

for strings and symbols we consider the text from the parent if :

  • the string or symbol starts with "["
  • the parent is an expr (not sure if necessary after testing for above, to be safe)
  • the parent has a single child (to dismiss some corner cases)

New behaviour:

x <- paste0("fun('", strrep("-", 1000), "')")
prettycode::highlight(x)
#> [1] "fun('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------')"

Created on 2022-11-10 with reprex v2.0.2

There are still corner cases unfortunately, see below in the parse data how the expression combines children so cannot be used as is.

I didn't go through the rabbit hole for those, they're unlikely to be encountered in practice. we keep the previous behavior.

code <- sprintf("b$'%s'", strrep("a", 1000))
pd <- getParseData(parse(text=code), includeText = TRUE)
pd$text <- substr(pd$text,1, 30)
pd
#>   line1 col1 line2 col2 id parent     token terminal
#> 5     1    1     1 1004  5      0      expr    FALSE
#> 1     1    1     1    1  1      3    SYMBOL     TRUE
#> 3     1    1     1    1  3      5      expr    FALSE
#> 2     1    2     1    2  2      5       '$'     TRUE
#> 4     1    3     1 1004  4      5 STR_CONST     TRUE
#>                             text
#> 5 b$'aaaaaaaaaaaaaaaaaaaaaaaaaaa
#> 1                              b
#> 3                              b
#> 2                              $
#> 4   [1000 chars quoted with ''']
prettycode::highlight(code)
#> [1] "b$[1000 chars quoted with ''']"

Created on 2022-11-10 with reprex v2.0.2

@gaborcsardi
Copy link
Member

Thanks, looks great! Can you please add an entry to the NEWS file?

@moodymudskipper
Copy link
Contributor Author

Done, thanks!

@gaborcsardi
Copy link
Member

Great, thank you!

@gaborcsardi gaborcsardi merged commit cf23660 into r-lib:main Nov 10, 2022
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.

highlight() trims long strings
2 participants