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

Allow Function for :src-dir #1679

Merged
merged 4 commits into from
Jun 23, 2021

Conversation

LaurenceWarne
Copy link
Contributor

@LaurenceWarne LaurenceWarne commented Jun 23, 2021

Hi! This PR is a follow up to #1671, it allows the :src-dir property of projects to be set to a function like :test-dir was in the previous PR.

Similar to to :test-dir, when projectile-toggle-between-implementation-and-test is called and the user is in a test file, and it's determined a project type has it's :src-dir property set to a function, this function will be called to obtain the directory in which the implementation file of the corresponding test file resides.

The result is that from the project in #1650 with the config:

(setq projectile-create-missing-test-files t)
(projectile-update-project-type
   'sbt
   :src-dir
   (lambda (file-path) (projectile-complementary-dir file-path "test" "main"))
   :test-dir
   (lambda (file-path) (projectile-complementary-dir file-path "main" "test")))

Is that calling projectile-toggle-between-implementation-and-test from:

  • src/main/scala/bar/package.scala sticks you in src/test/scala/bar/packageSpec.scala (regardless of the test file existing)
  • src/main/scala/foo/package.scala sticks you in src/test/scala/foo/packageSpec.scala (regardless of the test file existing)
  • src/test/scala/bar/packageSpec.scala sticks you in src/main/scala/bar/package.scala (if the latter file exists)
  • src/test/scala/foo/packageSpec.scala sticks you in src/main/scala/foo/package.scala (if the latter file exists)

Before submitting a PR make sure the following things have been done (and denote this
by checking the relevant checkboxes):

  • The commits are consistent with our contribution guidelines
  • You've added tests (if possible) to cover your change(s)
  • All tests are passing (eldev test)
  • The new code is not generating bytecode or M-x checkdoc warnings
  • You've updated the changelog (if adding/changing user-visible functionality)
  • You've updated the readme (if adding/changing user-visible functionality)

Thanks!

Make changes to projectile--find-matching file to the effect
that it will circumvent any existing logic if it is detected that the
src-dir property of the current project has been set to a function, in
which case, that function is called instead with the path of the test file.
Add tests for projectile--test-name-for-impl-name,
projectile--impl-file-from-src-dir-fn and add test for
projectile--find-matching-file to check precedence logic is as expected.
Document usage and behaviour of the src-dir option allowing a function
as a value.

| :test
| A command to test the project.

| :test-dir
| A path, relative to the project root, where the test code lives. A function may also be specified which takes one parameter - the directory of a file, and it should return the directory in which the test file should reside.
| A path, relative to the project root, where the test code lives. A function may also be specified which takes one parameter - the directory of a file, and it should return the directory in which the test file should reside. This option is only used for implementation/test toggling.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From looking at the source, please correct me if this is wrong.

@LaurenceWarne LaurenceWarne force-pushed the allow-function-for-src-dir branch from 7133f61 to 70ff939 Compare June 23, 2021 09:06
src-dir
#'projectile--impl-name-for-test-name)))
(when (file-exists-p impl-file)
(file-relative-name impl-file (projectile-project-root)))))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty much projectile--test-file-from-test-dir-fn but with the lookup reversed and it's also checked that the implementation file exists.

test-file (cl-remove-if-not predicate (projectile-current-project-files))))))
(if-let ((impl-file-from-src-dir-fn
(projectile--impl-file-from-src-dir-fn test-file)))
(list impl-file-from-src-dir-fn)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where :src-dir takes precendence if it's set to a function.

@bbatsov bbatsov merged commit 1fbe063 into bbatsov:master Jun 23, 2021
@bbatsov
Copy link
Owner

bbatsov commented Jun 23, 2021

Great work!

@LaurenceWarne
Copy link
Contributor Author

Awesome! Thanks for you ⚡ response

@ghost
Copy link

ghost commented Dec 6, 2021

Should this PR be resolved since it was merged? Also, does it resolve #1650 as well?

@LaurenceWarne
Copy link
Contributor Author

LaurenceWarne commented Dec 7, 2021

I've just pinged the creator of that issue to ask if he's happy that it's resolved.

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.

2 participants