Skip to content

index test_that() calls for fuzzy finder - #11102

Merged
kevinushey merged 14 commits into
rstudio:mainfrom
romainfrancois:feature/test_outline_11082
May 6, 2022
Merged

kevinushey merged 14 commits into
rstudio:mainfrom
romainfrancois:feature/test_outline_11082

Conversation

@romainfrancois

Copy link
Copy Markdown
Contributor

Intent

part of #11082

Approach

Index the calls to test_that() so that they are presented by the fuzzy finder:

image

Automated Tests

Indicate whether this change includes unit tests or integration tests, or link a work item or pull request to add those tests to another repo. If the change cannot or will not be covered by a test, indicate why.

QA Notes

Add additional information for QA on how to validate the change, paying special attention to the level of risk, adjacent areas that could be affected by the change, and any important contextual information not present in the linked issues.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

Comment thread src/cpp/core/r_util/RSourceIndex.cpp Resolved
@kevinushey

kevinushey commented May 2, 2022 •

Copy link
Copy Markdown
Collaborator

Nice! I think this (or a version of this) will be very useful. Some comments:

  • I'm a little worried that the sheer abundance of results here could overwhelm other relevant options in the fuzzy finder.
  • I think we should display only the inner desc string, without quotes.
  • We should also make sure to truncate long strings to some maximum size.

For the first point, one thing we could consider is allowing for a special "prefix" for matching test files. For example, you could type:

t mutate

to find all tests with a description matching "mutate". (This is modeled after Qt Creator's omni-search; we should consider discussing if we want to adopt this approach, and whether it might make sense to extend this to other unique prefixes, e.g. f file to only search files, $ name to only search for R functions / symbols, or something similar.)

Let's ask Hadley + others if they have any strong feelings.

Comment thread src/cpp/core/r_util/RSourceIndex.cpp Resolved
Comment thread src/cpp/core/r_util/RSourceIndex.cpp Resolved
Comment thread src/cpp/core/r_util/RSourceIndex.cpp Resolved
@romainfrancois

Copy link
Copy Markdown
Contributor Author

Interesting. it would be nice if we can come up with a syntax to restrict what is searched for.

t mutate : search for mutate in tests
!t mutate: search for mutate except in tests.

cc @hadley

We already have the : suffix to jump to specific line (& column) in a file:

image

This goes to line 10 of across.R.

@romainfrancois

Copy link
Copy Markdown
Contributor Author

We can also take advantage of that it is good practice to refer to the function that is tested using () i.e.

image

Not sure it would match other types of items.

@hadley

hadley commented May 3, 2022

Copy link
Copy Markdown
Member

I think including the test calls in the fuzzy file finder is much less compelling that including them in the document navigator, because I think most people don't really remember the names of tests. Having them in the navigator is useful because it helps you see the shape of tests in the one file; I suspect having them in the fff is likely to lead to false positives when you're trying to match other things.

I think it would be feasible if, as @kevinushey suggests, there was some way to select only tests, but I think that would necessitate a larger redesign of the fff, as you'd also need to expose the user to the filtering options in some way.

@romainfrancois

Copy link
Copy Markdown
Contributor Author

I've open another pr for the initial request: #11108

For this, I'd often want to jump to a test, but I vaguely remember where it is. I'd definitely use something like this, instead of what I'm doing currently: use find in files.

image

image

It's alright, but it's just less smooth.

But I agree that what I'm looking for here is some way to search within the tests only. Would rarely/never need them to be presented alongside other kinds of results.

@romainfrancois

This comment was marked as off-topic.

@romainfrancois
romainfrancois force-pushed the feature/test_outline_11082 branch 2 times, most recently from fe93e69 to 45fe3d8 Compare May 4, 2022 06:42
@romainfrancois

romainfrancois commented May 4, 2022 •

Copy link
Copy Markdown
Contributor Author

Changed so that tests are only shown if the search term starts with "t ".

So "t foo" will show:

  • tests fuzzy matching "foo"
  • other items fuzzy matching "t foo" (this should rarely be relevant)

image

And "foo" will show:

  • items fuzzy matching "foo" which are not test items

image

@romainfrancois
romainfrancois requested a review from kevinushey May 4, 2022 09:07
@kevinushey

Copy link
Copy Markdown
Collaborator

other items fuzzy matching "t foo" (this should rarely be relevant)

Would it be straightforward to disable that?

Overall LGTM; I think we should file separate issues regarding:

  • Other prefixes that we might want to consider for searching,
  • How we might want to document this, or increase its discoverability.

@romainfrancois

Copy link
Copy Markdown
Contributor Author

Would it be straightforward to disable that?

Yeah for sure, we could easily only display tests when the term starts with "t ". It's probably not a very relevant search term anyway. Let me just do that.

@romainfrancois
romainfrancois force-pushed the feature/test_outline_11082 branch from 864d302 to fe7cf9f Compare May 5, 2022 09:48
@romainfrancois

romainfrancois commented May 5, 2022 •

Copy link
Copy Markdown
Contributor Author

Now "t " only brings test_that() items, i.e. this also disables file results and c++ results.

The code might need some refactoring if we then decide to do other prefix later.

This was referenced May 5, 2022
Comment thread src/cpp/core/r_util/RSourceIndex.cpp Resolved
@romainfrancois
romainfrancois force-pushed the feature/test_outline_11082 branch from b9fad66 to b0b9fc1 Compare May 6, 2022 08:19
@romainfrancois

Copy link
Copy Markdown
Contributor Author

raw strings ✅

image

@kevinushey kevinushey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

if (clone.isType(RToken::STRING))
{
static const boost::regex removeQuotesRegex("^['\"](.*)['\"]$");
static const boost::regex removeRawStringQuotesRegex("^r['\"][(](.*)[)]['\"]$");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Raw strings in R are very permissive; for example:

> R"(abc)"
[1] "abc"
> R"{abc}"
[1] "abc"
> R"[abc]"
[1] "abc"
> R"--[abc]--"
[1] "abc"

That is, you can use r or R to begin a raw string, a variable number of - separators, and also your own choice of (, { or [. See for example:

Error RTokenizer::matchRawStringLiteral(RToken* pToken)
{
auto start = pos_;
// consume leading 'r' or 'R'
wchar_t firstChar = eat();
if (!(firstChar == L'r' || firstChar == L'R'))
{
pos_ = start;
return tokenizeError(
"expected 'r' or 'R' at start of raw string literal",
ERROR_LOCATION);
}
// consume quote character
wchar_t quoteChar = eat();
if (!(quoteChar == L'"' || quoteChar == L'\''))
{
pos_ = start;
return tokenizeError(
"expected quote character at start of raw string literal",
ERROR_LOCATION);
}
// consume an optional number of hyphens
int hyphenCount = 0;
wchar_t ch = eat();
while (ch == L'-')
{
hyphenCount++;
ch = eat();
}
// okay, we're now sitting on open parenthesis
wchar_t lhs = ch;
// form right boundary character based on consumed parenthesis.
// if it wasn't a parenthesis, just look for the associated closing quote
wchar_t rhs;
if (lhs == L'(')
{
rhs = L')';
}
else if (lhs == L'{')
{
rhs = L'}';
}
else if (lhs == L'[')
{
rhs = L']';
}
else
{
pos_ = start;
return tokenizeError(
"expected opening bracket at start of raw string literal",
ERROR_LOCATION);
}
// start searching for the end of the raw string
bool valid = false;
while (true)
{
// i know, i know -- a label!? we use that here just because
// we need to 'break' out of a nested loop below, and just
// using a simple goto is cleaner than having e.g. an extra
// boolean flag tracking whether we should 'continue'
LOOP:
if (eol())
break;
// find the boundary character
wchar_t ch = eat();
if (ch != rhs)
goto LOOP;
// consume hyphens
for (int i = 0; i < hyphenCount; i++)
{
ch = eat();
if (ch != L'-')
goto LOOP;
}
// consume quote character
ch = eat();
if (ch != quoteChar)
goto LOOP;
// we're at the end of the string; break out of the loop
valid = true;
break;
}
// update position
std::size_t row = row_;
std::size_t column = column_;
updatePosition(start, pos_ - start, &row_, &column_);
// set token and return success
auto type = valid ? RToken::STRING : RToken::ERR;
*pToken = RToken(type, start, pos_, start - data_.begin(), row, column);
return Success();
}

Rather than a regular expression, because we know the token must already be a string, I think you should do something like the following:

  1. Check for a leading r or R for a raw string versus a "regular" string,
  2. Find the index of the first character of "({[",
  3. Use that to extract the inner substring of the raw string, e.g. content.substr(<start>, <end>) where both <start> and <end> are computed based on the size of the raw string prefix.

In practice I doubt any user is going to use raw strings in this context, and even if they do the worst case is they see the raw string as-is in the UI, so that's probably not the end of the world.

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.

Thanks. I'll forget about this until someone someday uses a raw string in a test_that() description.

@kevinushey
kevinushey merged commit 1870ca0 into rstudio:main May 6, 2022
@romainfrancois
romainfrancois deleted the feature/test_outline_11082 branch May 6, 2022 19:04
kevinushey added a commit that referenced this pull request May 6, 2022
* show test_that() calls in document outline

* use test icon

* style test items in the outline

* +NEWS

* Make function assignments secundary in test files

* nodeLabelSecundary not italic

* hyperlink get no default color (#11147)

* update rstheme

* update expected rstheme

* 🇫🇷 for "plot" is "graphique" (#11148)

* update plot french text

* more

* tracé -> graphique

* plural

* `uiLanguage*` properties in fr (#11119)

* uiLanguage* properties in french

* add trainling : in fr and en

* visual editor: auto-prefix footnotes in included files

support for 'editor' metadata options in files and projects

* index `test_that()` calls for fuzzy finder (#11102)

* index test_that calls for fuzzy finder

* use moveToNextSignificantToken()

* Don't unclude test_that() in the string

* rm quote from test desc

* make removeQuoteRegex static const

* testThatCAllIndexer adds "t " prefix

* only show test items if the search term starts with "t "

* fuzzy finder removes artifical "t " prefix

* remove dead code

* When search term starts with "t ", only include test items

* not looking for files and C++ items when only looking for test items

* NEWS

* factor out includeTestItems

* handle raw string in test_that(<raw string>)

* Update src/gwt/acesupport/acemode/r_scope_tree.js

Co-authored-by: Kevin Ushey <[email protected]>

* show test_that() calls in document outline

* use test icon

* style test items in the outline

* +NEWS

* Make function assignments secundary in test files

* nodeLabelSecundary not italic

* use opacity 0.8

* opacity: .7

* .nodeLabelSecundary:hover

Co-authored-by: JJ Allaire <[email protected]>
Co-authored-by: Kevin Ushey <[email protected]>
@romainfrancois romainfrancois mentioned this pull request May 24, 2022
4 tasks
@olivroy olivroy mentioned this pull request Jun 12, 2024
1 of 4 tasks
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