Code Beautifier 1.0 (PythonScript)
-
Code Beautifier 1.0
Maintaining clean and organized code is paramount for readability, collaboration, and overall efficiency. However, manually formatting code can be time-consuming and error-prone. This is where the Code Beautifier script can lend a hand, designed to effortlessly enhance the structure and appearance of your code within Notepad++. Feel free to use any parts of the code in the script as long as its for opensource purposes.
Features:
-
Language-Specific Settings: Code Beautifier adapts to all current and future programming languages supported in Notepad++, including User Defined Languages. Allowing you to customize indentation style, spacing preference, and comment characters for each language.
-
Keyword Grouping: Organize your code by categorizing keywords into logical groups, such as “IndentLeft”, “IndentRight”, “IndentBoth” and “IndentNone”. Code Beautifier provides a seamless interface for managing these keyword groups.
-
Automatic Code Formatting: Simply by saving your file, Code Beautifier automatically analyzes your code and applies the predefined formatting rules based on the language settings, significantly reducing manual effort and potential errors.
-
Keyword capitalization: On top of checking the indentation of your code, it will also capitalize any keywords entered in the “Indent” groups in the language settings window. Any keyword found in the code page will be capitalized exactly as it is written in the keyword groups settings, further enhancing readability.
-
Intuitive GUI: The graphical user interface (GUI) of Code Beautifier provides a user-friendly environment for configuring language-specific settings, making it a breeze to configure.
README:
1. Installation:
- Ensure you have Notepad++ installed on your system.
- Download and install the PythonScript plugin for Notepad++ if you haven’t already.
- Copy the source of the script from the Github link and paste it in a new *.py file called “code.beautifier”.
- Place the script file in the appropriate folder in your Notepad++ installation directory.
“plugins\Config\PythonScript\scripts”
2. Using Code Beautifier:
- Open Notepad++ and navigate to the PythonScript menu.
- Select “Scripts” and then choose code beautifier from the list. Or you can choose to configure Notepad++ to load the script “ATSTARTUP” in the PythonScript “Configuration”, That way you don’t have to run it yourself each time when you start up Notepad++.
- The script will now run in the background while you are working, and when you have configured settings for the language you’re using it will beautify the code based on your chosen settings when you save your file.
- When you change languages or change document in Notepad++, the script will check the “plugins\Config\PythonScript\scripts” folder if there is folder called “Code Beautifier” and if a settings file exists for this language and if not create a folder and settings file.
3. Configuring Language Settings:
- Type the keyword “code_beautifier_settings” in any document in Notepad++ while the script is running and you’ll be presented with a window with buttons for each letter of the alphabet.
- Click on the letter corresponding to the language you’re working with.
- This action will open another window displaying a list of languages starting with the selected letter.
- Click on the desired language to proceed.
4. Indentation Groups Explained
- IndentRight: when a keyword that belongs to this group is detected in the codepage, pushes the next line in the editor 1 indent level to the right.
- IndentLeft: when a keyword that belongs to this group is detected in the codepage, pulls the current line of the keyword in the editor 1 indent level to the left.
- IndentBoth: when a keyword that belongs to this group is detected in the codepage, pulls the current line of the keyword in the editor 1 indent level to the left and pushes the next line in the editor 1 indent level to the right.
- IndentNone: when a keyword that belongs to this group is detected in the codepage, does nothing in terms of indentation but is used for the capitalization of keywords feature.
5. Customizing Settings:
- In the language-specific settings window, you can customize various aspects such as indentation style, spacing preference, and comment characters.
- Use the provided options and input fields to tailor the settings according to your preferences.
- When entering keywords in the text widgets, the keywords should be in a long list, each keyword on a new line.
- Comment characters should be separated by a space or a comma,
6. Saving Settings:
- No interaction is needed for saving settings, the script saves on losing focus and closing of the language-specific window.
7. Automated Code Formatting:
- Once all your settings are configured for the language you are using, simply saving the document you are working on will apply the beautification process.
8. Media:
9. Source:
-
-
thanks, the best plugin for python programmers
-
@Khundian-Twitch ,
Sorry, not seeing your app working, as it appears from the pythonscript console, that you are calling a Tkinter module, which I never downloaded/installed and which your instructions are moot on. Here’s the readout I got in the console.Registered DotLogTimestamp.py callbacks Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] Initialisation took 94ms Ready. Traceback (most recent call last): File "C:\Users\<username>\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 19, in <module> import Tkinter as tk ImportError: No module named Tkinter Registered DotLogTimestamp.py callbacks Traceback (most recent call last): File "C:\Users\<username>\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 19, in <module> import Tkinter as tk ImportError: No module named Tkinter Traceback (most recent call last): File "C:\Users\<username>\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 19, in <module> import Tkinter as tk ImportError: No module named Tkinter
If you require a module to be loaded, either you need to include it, or at the least, state it is needed and where to get it.
:-( -
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
If you require a module to be loaded, either you need to include it, or at the least, state it is needed and where to get it.
Tkinter is part of PythonScript itself; for example, see the downloads from https://github.com/bruderstein/PythonScript/releases that have “Tk” in their names:
-
@Alan-Kilborn ,
Unfortunately, unless I’ve done something wrong, that doesn’t show up as part of the PythonScript libary modules. I checked. It’s in my standalone 3.10 Python, but it’s not installed as part of the Python27.dll or any of the files in the lib of that install in the PythonScript that is in NPP’sC:\Program Files\Notepad++\plugins\PythonScript\lib
.If I’ve messed something when installing the PythonScript plugin, I don’t know what it can be, because the PythonScript script files work, except for this one requiring the Tkinter module. ::shrug::
However, looking at your list of files, perhaps PythonScript has an upgrade that I haven’t installed, and since the Plugin manager hasn’t indicated an upgrade is available, I haven’t upgraded…so could that be the problem? And if so, what changes am I looking at having to make with regard scripts that are already usable in NPP, and what files/how to migrate to a newer version? Thanks in advance @Alan-Kilborn .
-
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
it’s not installed as part of the Python27.dll or any of the files in the lib of that install in the PythonScript that is in NPP’s C:\Program Files\Notepad++\plugins\PythonScript\lib.
Correct.
I don’t bother with installers when I don’t have to, so I’ve never installed PythonScript other than manually. So…I’m not 100% sure but I’d think that if you install it using an installer, you get the “Full” version (“full” meaning not everything but only what the PS maintainers consider “full”) from the list of files I presented in my last post. And “full” does NOT include Tkinter.
To get Tkinter, you MUST install that manually, by extracting a “TclTk” zip file into the appropriate folder of your existing PS plugin directory structure.
looking at your list of files, perhaps PythonScript has an upgrade that I haven’t installed
My intent was to just show how the files are distributed, not any specific version.
I showed version 3 of PS because that’s the one I use.
The only PS version currently available via Plugins Admin is PS2 (apparently that is the version its developer’s recommend for most users).And…note that if you’re using PS2, you (somewhat obviously) need the “TckTk” file appropriate to that version.
-
@Alan-Kilborn ,
Thanks, so that means I have to track down a 2.7 version Tkinter module file, since what I’ve read, 2.7 has been discontinued since 2002 for development. I was reading that every version is a pre release, and no mentions of any stable version since 2.7 so I’m guessing that’s why the developer decided to stay with it.
Thanks again. It helps, since I’m not a regular Python or PythonScript guy, I just need it to run other peoples work. :-) -
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
…that means I have to track down…
Is it really that hard to find? Try HERE.
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
2.7 has been discontinued
Very true.
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
I’m guessing that’s why the developer decided to stay with it (Python 2.7)
There are special reasons that PythonScript is in somewhat of a split personality state. PS2 is great for those that work with files with legacy encoding (called ANSI in Notepad++ terminology) and it is harder to work with modern (Unicode) files. PS3 is perfect for Unicode, but not compatible at all with ANSI files.
-
@Alan-Kilborn ,
Thanks. I did find it while this window was open, and did an install and followed the instructions to find out if the tkinter is installed correctly, but the installer has no specific option to install tkinter and the IDLE, even though I’ve checked them in the installer that is supposed to install them all. I then look and I see…_tkinter
in the lib directory, so I know it’s there, I’m guessing that the installer doesn’t install it per se, so I’m trying to figure out what to do next to get it just to work with a Python install, let alone the NPP PythonScript.Be back if I’m still having problems with it, but I think I’m about done for the night…can’t play anymore. :( But this is what I’m getting in the console now, so something has changed after I copied the lib directories of the package in to the plugin directory under the install directory. I have no idea what it means, but will play again tomorrow.
Registered DotLogTimestamp.py callbacks Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] Initialisation took 172ms Ready. Traceback (most recent call last): File "C:\Users\camilee\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 140, in <module> load_language_settings() File "C:\Users\camilee\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 98, in load_language_settings os.makedirs(folder_path) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 150, in makedirs makedirs(head, mode) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 150, in makedirs makedirs(head, mode) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 157, in makedirs mkdir(name, mode) WindowsError: [Error 5] Access is denied: 'plugins/Config/PythonScript' Traceback (most recent call last): File "C:\Users\camilee\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 140, in <module> load_language_settings() File "C:\Users\camilee\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\code.beautifier.py", line 98, in load_language_settings os.makedirs(folder_path) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 150, in makedirs makedirs(head, mode) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 150, in makedirs makedirs(head, mode) File "C:\Program Files\Notepad++\plugins\PythonScript\lib\os.py", line 157, in makedirs mkdir(name, mode) WindowsError: [Error 5] Access is denied: 'plugins/Config/PythonScript'
-
@Lycan-Thrope @Alan-Kilborn
Hey everyone!I’m surprised that the script doesn’t work out of the box, I just downloaded a portable version of Notepad++ v8.6.4 and installed the PythonScript 2 plugin from the “Plugins Admin” menu. Created a new script file with the Code Beautifier code and ran it. Everything is working on my end without any external or manual downloads, normally the Tkinter module should be available in the standard PythonScript 2 plugin.
I haven’t tested the script with Python3 yet, keep in mind the “Tkinter” module is named different in both versions of Python.
Python2 = Tkinter
Python3 = tkinter -
@Khundian-Twitch said in Code Beautifier 1.0 (PythonScript):
…normally the Tkinter module should be available in the standard PythonScript 2 plugin.
This is not correct…or, at least, it’s not my experience.
I just tried installing the PythonScript plugin (v.2.0.0.0) via Plugins Admin into a fresh Notepad++.
Then I tried to import Tkinter (or even tkinter) in the PS console:It’s pretty clear that it isn’t there with a Plugins admin install.
-
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
did an install and followed the instructions to find out if the tkinter is installed correctly, but the installer has no specific option to install tkinter and the IDLE, even though I’ve checked them in the installer that is supposed to install them all. I then look and I see… _tkinter in the lib directory, so I know it’s there, I’m guessing that the installer doesn’t install it per se, so I’m trying to figure out what to do next to get it just to work with a Python install, let alone the NPP PythonScript.
To be clear, nothing I’m talking about is relating to getting Tkinter into a non-PythonScript situation (i.e., getting it into a standard/standalone Python). Discussion of that is really outside of the scope of this forum and should be discussed elsewhere, if needed.
So…if we’re talking only about PythonScript… (and we are):
First (and I thought this was totally clear from earlier posting where I said To get Tkinter, you MUST install that manually, by extracting a “TclTk” zip file into the appropriate folder of your existing PS plugin directory structure), there is no installer for Tkinter on PS. Period. End of story. Let’s stop talking about installers (ugh!).
What you want to do is to grab
PythonScript_TclTk_2.0.0.0_x64.zip
(if using 64-bit N++) and unzip that into your N++ folder (where you can see the existingplugins
folder in it).Restart Notepad++ and try an
import Tkinter
in the PS console window and it should not fail (i.e. no red error messages):At that point you should (obviously) be good to go from a Tkinter standpoint.
I don’t know what problems you’ll encounter (if any) with the Code Beautifier script, as I haven’t tried that myself. -
@Alan-Kilborn @Lycan-Thrope
Alan is right, I forgot I have the full Python 2.7.18 release installed on my workstation. Hence the reason why it worked out of the box for me. Apologies for the confusion.You can install/enable the Tkinter module as Alan described above or you can install the full Python 2.7.18 release version from here. Either of these options should enable you to use the Tkinter module.
-
@Khundian-Twitch A relative path of
settings_folder = "plugins/Config/PythonScript/scripts/Code Beautifier" # Folder containing language settings files
will operate in the current working directory (cwd). The cwd can be the root of where notepad.exe is located. This may work for portable, though Notepad++ installed in
C:\Program Files\Notepad++
can cause the errorWindowsError: [Error 5] Access is denied
as unable to create folders or files in that location.I suggest an absolute path to the plugin config directory to ensure a writable location:
settings_folder = os.path.join(notepad.getPluginConfigDir(), r"PythonScript\scripts\Code Beautifier") # Folder containing language settings files
The change suggested is with line 28 .
-
@Khundian-Twitch said in Code Beautifier 1.0 (PythonScript):
you can install the full Python 2.7.18 release version
But you neglected to say that you must change your PS Configuration in order to use this. I’m not on my PC right now, so I can’t give the exact name of the setting for this.
It is best, when possible, to NOT demand that users have a standalone Python on their system in order to use scripts with Notepad++ – users that don’t already do Python are probably 0% likely to do this anyway. In this case (for the script in this thread), it is possible, as Tkinter does have a PS way to be run (discussed ad nauseum in this thread already).
-
@mpheath said in Code Beautifier 1.0 (PythonScript):
@Khundian-Twitch A relative path of
settings_folder = "plugins/Config/PythonScript/scripts/Code Beautifier" # Folder containing language settings files
will operate in the current working directory (cwd). The cwd can be the root of where notepad.exe is located. This may work for portable, though Notepad++ installed in
C:\Program Files\Notepad++
can cause the errorWindowsError: [Error 5] Access is denied
as unable to create folders or files in that location.I suggest an absolute path to the plugin config directory to ensure a writable location:
settings_folder = os.path.join(notepad.getPluginConfigDir(), r"PythonScript\scripts\Code Beautifier") # Folder containing language settings files
The change suggested is with line 28 .
Thanks for the suggestion, when I have some time I’ll have a look at the script to ensure compatibility with the installer version of Notepad++. Probably over the weekend.
@Alan-Kilborn said in Code Beautifier 1.0 (PythonScript):
@Khundian-Twitch said in Code Beautifier 1.0 (PythonScript):
you can install the full Python 2.7.18 release version
But you neglected to say that you must change your PS Configuration in order to use this. I’m not on my PC right now, so I can’t give the exact name of the setting for this.
It is best, when possible, to NOT demand that users have a standalone Python on their system in order to use scripts with Notepad++ – users that don’t already do Python are probably 0% likely to do this anyway. In this case (for the script in this thread), it is possible, as Tkinter does have a PS way to be run (discussed ad nauseum in this thread already).
I agree that your method is preferred over installing a standalone version of Python, but I didn’t have to change any PythonScript configuration to be able to use Tkinter.
-
@Alan-Kilborn,
Thanks @Alan-Kilborn . I apparently went awry somewhere along the way, followed your instructions, and had to retrace the installation of the script’s placement in the NPP directory, rather than the user directory, and then I changed the path at line 28, restarted NPP, and although I still had a problem getting it to work, I finally put the “quotes” around the keyword and got it to finally show the UI aspects. I’ll play more later.This brings up, @Khundian-Twitch , that you have to write instructions for an idiot like me, so I don’t screw them up. Which means you need to (as I did for my own dBASEPlus installer) assume nothing and either make it so the install is brain dead simple, or the instructions are written to a neophyte understanding level. As @Alan-Kilborn points out, the more difficult and detailed and with missing details the process is, the less likely you’ll have users to use it.
I’m a programmer, but not a Python or PythonScript programmer. I only use it because the scripts you guys write, need them to work, otherwise I’d be fine not bothering with Python at all. I’m slowly looking to do some programming in Python/PythonScript eventually, but not today. :-) Write for the idiot in us. :-)
-
@Khundian-Twitch ,
Have an issue I’m not sure of if is your plug in issue. I have two dBASE UDL’s. One for DarkMode and one for non-DarkMode. Named differently and when I tried to trigger the Beautifier in the non-DarkMode, it ignores it as another UDL. It would be in the same Alphabetic lettering, but is named, dBASEPlus and the DarkMode is…of course Dark_dBASEPlus. Only the DarkMode version is showing up in your Alphabetic listing.
Just an FYI. -
@Lycan-Thrope said in Code Beautifier 1.0 (PythonScript):
@Khundian-Twitch ,
Have an issue I’m not sure of if is your plug in issue. I have two dBASE UDL’s. One for DarkMode and one for non-DarkMode. Named differently and when I tried to trigger the Beautifier in the non-DarkMode, it ignores it as another UDL. It would be in the same Alphabetic lettering, but is named, dBASEPlus and the DarkMode is…of course Dark_dBASEPlus. Only the DarkMode version is showing up in your Alphabetic listing.
Just an FYI.Thanks for reporting your issue.
The script doesn’t pick up the non-dark version because “D” doesn’t equal “d”, make sure your language name starts with a capital letter and it will work without an issue.
When I have some time off, I’ll have a look at the script to make it case insensitive.
-
@Khundian-Twitch said in Code Beautifier 1.0 (PythonScript):
The script doesn’t pick up the non-dark version because “D” doesn’t equal “d”, make sure your language name starts with a capital letter and it will work without an issue.
When I have some time off, I’ll have a look at the script to make it case insensitive.
Thanks for verifying that, and taking a look to possibly change it. It actually is the products proper name, “dBASE”, so it’s more recognizable to people that know about it. Dark_ was just added so they could know the difference and setup their color scheme differently depending on the option they chose.
They use the same functionList and overrideMap .xml files, but each version has it’s own autoComplete.xml file and UDL language .xml file for coloring accordingly. Still playing and finding things out as I go. :-)