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

NimPretty has troubles with source code filter #9384

Closed
technicallyagd opened this Issue Oct 16, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@technicallyagd
Copy link
Contributor

technicallyagd commented Oct 16, 2018

If you try to format files that use source code filter, nimpretty would generate two different kinds unexpected output depending on whether the operation is performed in-place.

For the simplest example found in the docs:

#? stdtmpl(subsChar = '$', metaChar = '#')
#proc generateXML(name, age: string): string =
#  result = ""
<xml>
  <name>$name</name>
  <age>$age</age>
</xml>

If in-place (i.e. -o:FILE is the same as input):



If output is a different file:



proc generateXML*(name, age: string): string =
  result = L*
  result.add(ring): st &
      result = & $(name) & name>$name< &
     <age>$ag & $(age) & l>
&
    )

I am guessing nimpretty is not meant to format source code filters, but it would be nice if it could recognize source code filters and just leave them alone, since it makes editing source code filters really tedious on IDE's, that use nimpretty to format code, with auto-formatting turned on.

@Araq

This comment has been minimized.

Copy link
Member

Araq commented Oct 16, 2018

Source code filters have their own file extension, you can invest a tiny bit of logic into your IDE configuration.

@technicallyagd

This comment has been minimized.

Copy link
Contributor

technicallyagd commented Oct 16, 2018

Really? That would be nice! But I don't think it's mentioned in the docs though

@technicallyagd

This comment has been minimized.

Copy link
Contributor

technicallyagd commented Oct 17, 2018

Well, since @Araq seems to be working hard fixing more important issues, I am going to answer my own question. After digging through the source code, I found that niminst (docs) uses source code filter extensively. The conventional file extension seems to be .tmpl, and to use it, one needs to call

include "xxx.tmpl"

before calling the procedure you defined in xxx.tmpl.

I said "conventional" because, after some testing, the file extension doesn't seem to matter, you can use .puppy or .unicorn, as long as you call include "xxx.unicorn", it will work.

However, if you use .nim extension for the source code filter file, and import xxx in your code, the procedure defined would also be available if you have exported it. Personally, I like this approach a bit more, since I have some irrational fear of using include in my code, and using .nim gives me better syntax highlighting for my current IDE setup. In the end, I just modified my local nimpretty to check whether the input file contains #? in its first line, and abort if it does. I guess you can do the same in the IDE extension code before calling nimpretty.

I really like this source code filtering feature. It's a shame that not much people are using it. I will try to make a PR to update the documentation with the above info.

@Araq

This comment has been minimized.

Copy link
Member

Araq commented Oct 17, 2018

In the end, I just modified my local nimpretty to check whether the input file contains #? in its first line, and abort if it does.

Create a PR for this please.

@technicallyagd

This comment has been minimized.

Copy link
Contributor

technicallyagd commented Oct 17, 2018

@Araq, okay, coming up next.

@technicallyagd

This comment has been minimized.

Copy link
Contributor

technicallyagd commented Oct 17, 2018

Done. I will update PR #9407 after PR #9408 is merged to reflect the change.

@Araq Araq closed this in bd9d578 Oct 17, 2018

narimiran added a commit to narimiran/Nim that referenced this issue Oct 31, 2018

narimiran added a commit to narimiran/Nim that referenced this issue Nov 1, 2018

narimiran added a commit that referenced this issue Nov 1, 2018

narimiran added a commit that referenced this issue Nov 1, 2018

nimpretty: fixes #9384
(cherry picked from commit bd9d578)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment