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

TeX source-formatting script #957

Closed
MeanSquaredError opened this issue Feb 22, 2020 · 20 comments
Closed

TeX source-formatting script #957

MeanSquaredError opened this issue Feb 22, 2020 · 20 comments

Comments

@MeanSquaredError
Copy link
Contributor

MeanSquaredError commented Feb 22, 2020

I finished implementing the first proof-of-concept TeX source-formatting script. The script requires a recent development version of TXS (3.0.0-git) because it uses the new document.getLineTokens() method.

It is implemented as a .txsMacro so it can be imported from
Macros/Edit Macros.../Import

It can be used by opening a .tex document in the editor and then starting the imported macro from the Macros menu item.

I am pretty sure that it has quite a few rough edges, since it is the very first version and I only gave it very limited testing. So any testing and feedback are welcome!

format_source.txsMacro.txt

@sunderme
Copy link
Member

you can add it to texstudio-macro which makes import even simpler ...

@MeanSquaredError
Copy link
Contributor Author

OK, I created a PR with this script in the texstudio-macro repository.

@sunderme
Copy link
Member

The script seems to destroy my text, see issue

@MeanSquaredError
Copy link
Contributor Author

The issue should be fixed now. The script that is attached to this post is already obsolete, but the latest version can be downloaded from the TXS menu Macros/Edit Macros.../Browse then AutomatedTextmanipulation/formatTexSource.txsMacro

@dbitouze
Copy link
Contributor

dbitouze commented Apr 14, 2020

@MeanSquaredError Many thanks for the work on this subject!

From:

\documentclass{article}
\begin{document}
Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla 
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\end{document}

I get:

\documentclass{article}
\begin{document}
	Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\end{document}

whereas I would expect:

\documentclass{article}
\begin{document}
Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla 
\end{document}

Am I missing something?

@MeanSquaredError
Copy link
Contributor Author

@dbitouze

The script assumes soft line wrapping and from your output it seems that you are using hard line wrapping.

Regarding the indentation - the latest version of the script supports a list of environments for which no indentation should be applied. By default it applies indentation to all environments. The script posted at the beginning of this issue is an old version, but you can get the latest version of the script either from the TXS builtin script importer or from
https://github.com/MeanSquaredError/texstudio-format-source/blob/master/scripts/format_source.txt
Then you can change the configuration on lines 6-7 and disable indentation for the document environment. E.g.

config = {
	// Names of environments that do not indent their children
	noIndent: ['document'],
	...

I will try to update the script so that it will autodetect the line wrapping settings and will split the lines if hard line wrapping is configured. Hopefully it should happen in the next couple of days :-)

@dbitouze
Copy link
Contributor

@MeanSquaredError

The script assumes soft line wrapping and from your output it seems that you are using hard line wrapping.

Nothing escapes you! 😄

Regarding the indentation - the latest version of the script supports a list of environments for which no indentation should be applied. By default it applies indentation to all environments. The script posted at the beginning of this issue is an old version, but you can get the latest version of the script either from the TXS builtin script importer

Do you mean Macros → Edit Macros → Import? If I click on the latter, it just opens my File Manager...

or from
https://github.com/MeanSquaredError/texstudio-format-source/blob/master/scripts/format_source.txt
Then you can change the configuration on lines 6-7 and disable indentation for the document environment. E.g.

config = {
	// Names of environments that do not indent their children
	noIndent: ['document'],
	...

OK, done! 😄

I will try to update the script so that it will autodetect the line wrapping settings and will split the lines if hard line wrapping is configured. Hopefully it should happen in the next couple of days :-)

Thank you very much!

@MeanSquaredError
Copy link
Contributor Author

@dbitouze
Sorry, by "import" I meant Macros/Edit Macros then pressing the Browse button.
After that you can navigate to automatedTextManipulation and get formatTexSource.txsMacro from there. I did not notice that there is already an Import button which browses the local filesystem.

However if you already got the script from my github account then you are already settled, since it is the latest version of the script anyway.

@dbitouze
Copy link
Contributor

@MeanSquaredError

Sorry,

You're welcome!

by "import" I meant Macros/Edit Macros then pressing the Browse button.
After that you can navigate to automatedTextManipulation and get formatTexSource.txsMacro from there.

Okay. This way of updating remote macros is far from obvious: is some more straightforward way planned?

@MeanSquaredError
Copy link
Contributor Author

MeanSquaredError commented Apr 14, 2020

@dbitouze

Okay. This way of updating remote macros is far from obvious: is some more straightforward way planned?

This mechanism for macro updates has been there for a while (definitely before I started submitting TXS patches), what it does effectively is that it allows the user to fetch macros from
https://github.com/texstudio-org/texstudio-macro

I am not really familiar with any plans for any changes to this macro update feature. My guess is that there are no specific plans for any changes to it. However I think that any comments/suggestions are welcome (whether they will be implemented is another matter and depends mainly if there will be someone willing to work on the suggestions :-)

@MeanSquaredError
Copy link
Contributor Author

@dbitouze

OK, I implemented support for hard line wrapping in the source formatting script. Now it will autodetect the configured line wrapping mode and will format the source accordingly.
You can download the script from:

https://github.com/MeanSquaredError/texstudio-format-source/blob/master/scripts/format_source.txt

It has not been uploaded to the texstudio-macro repository yet, but if it works correctly for you, then I will upload it there too.

@dbitouze
Copy link
Contributor

dbitouze commented Apr 19, 2020

@MeanSquaredError With:

  • TeXstudio 3.00.00 (git 3.0.0alpha3-24-g7851be67)
  • Qt version 5.12.6, compiled with Qt 5.12.6 D

and the new version of the script (526 lines), I still get the text on a single line.

I don't know how to debug in order to help you: maybe my texstudio.ini file could help?

@MeanSquaredError
Copy link
Contributor Author

@dbitouze

Yes, please upload you texstudio.ini.

For the new script to split the text on multiple lines it need the following settings:
Adv. Editor / Line wrapping set to Hard Line Wrap after max. Characters
Adv. Editor / Maximal Characters set to a number greater than zero.

@dbitouze
Copy link
Contributor

Hum! Sorry for the noise:

  • Adv. Editor / Line wrapping was set to No Line Wrap,
  • Adv. Editor / Maximal Characters was set to 80.

With the settings you specified (and that are obvious), it works nicely!

Hence, maybe a warning could be emitted if the required settings are not fullfilled

@MeanSquaredError
Copy link
Contributor Author

@dbitouze
The script tries to autodetect user settings and to adjust the source accordingly.
For example I am used to working with soft line wrapping and I prefer the script to put all the text on a single line because when soft line wrapping is enabled TXS splits the text on its own.

I guess I could add an additional configuration option that would allow the user to specify explicitly way text is handled instead of using the TXS settings.

@MeanSquaredError
Copy link
Contributor Author

@dbitouze
@muzimuzhi

OK, I added support for configurable column number after which hard line wrapping occurs. The configuration setting is specified config.hardWrapColumn. The default value is autodetect

	// Line number after which hard wrap is performed by insertion of newline character(s).
	// Can be one of:
	//	'autodetect': to autodetect the setting based on TeXstudio settings.
	//	'none': to disable hard line wrapping. This will cause text nodes to be placed on a single line. Useful if TexStudio uses soft line wrapping.
	//	any positive integer value (e.g. 80): to specify explicitly the line number after which the newline character(s) will be inserted.
	// Note that this value only affects text content that may be broken along whitespace. In some cases when there is no whitespace in the text content
	// a line may extend past the specified hard wrap column.
	hardWrapColumn: 'autodetect'

The latest version of the script can be downloaded from https://github.com/MeanSquaredError/texstudio-format-source/blob/master/scripts/format_source.txt but I also sent a PR to texstudio-macro, so it should appear there as well in a day or so.

@dbitouze
Copy link
Contributor

@MeanSquaredError Works nicely, thanks!

Nevertheless, I would again emphasize that this should be a built-in feature of TXS: it is such a pain to localize (La)TeX errors in too long lines...

@MeanSquaredError
Copy link
Contributor Author

MeanSquaredError commented Apr 22, 2020

@dbitouze
As far as I understand adding this as a built-in feature to TXS depends on two things:

  1. If there are enough users of this feature. So far based on user feedback I see one and a half users of this feature: you and (maybe) me :-)
  2. If there is someone willing to spend his time and integrate this feature into TXS. While working on the source-formatting script I found a bunch of edge cases (not really related to the source-formatting script) where a regular user is able to make TXS crash. So I would rather spend my free time on preparing bugreports and fixes for these crashes first. Once these crashes are fixed and there is enough interest (i.e. users) in the source formatting as a built-in feature I guess we can discuss its integration in the main TXS code.

@dbitouze
Copy link
Contributor

dbitouze commented Apr 22, 2020

@MeanSquaredError

As far as I understand adding this as a built-in feature to TXS depends on two things:

  1. If there are enough users of this feature. So far based on user feedback I see one and a half users of this feature: you and (maybe) me :-)

;) IMHO, users don't know they need this killing feature because they think looking for (La)TeX errors in veeeeeeeeeeeeery long lines, just as looking for a needle in a haystack, is just a normal inconvenient of LaTeX.

  1. If there is someone willing to spend his time and integrate this feature into TXS. While working on the source-formatting script I found a bunch of edge cases (not really related to the source-formatting script) where a regular user is able to make TXS crash.

Damn! :)

So I would rather spend my free time on preparing bugreports and fixes for these crashes first.

I can understand.

Once these crashes are fixed and there is enough interest (i.e. users) in the source formatting as a built-in feature I guess we can discuss its integration in the main TXS code.

The point is how to warn them about the necessity to vote for this feature request :$

BTW, maybe the current macro could be extended or, rather, changed to format only the current selected portion of text. Hence users wouldn't complain that all the 5749 lines of their document has been formatted by error...

@MeanSquaredError
Copy link
Contributor Author

@dbitouze
The ability to format only the selected area is definitely on my TODO list. Actually the lack of this feature is probably the main reason why I don't use the source formatting script too often myself :-)

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

No branches or pull requests

3 participants