initial commit
This commit is contained in:
4
Packages/Prettierd Format/.gitattributes
vendored
Normal file
4
Packages/Prettierd Format/.gitattributes
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/images/ export-ignore
|
||||
/tests/ export-ignore
|
||||
*.sublime-project export-ignore
|
||||
*.sublime-workspace export-ignore
|
||||
0
Packages/Prettierd Format/.no-sublime-package
Normal file
0
Packages/Prettierd Format/.no-sublime-package
Normal file
10
Packages/Prettierd Format/LICENSE
Normal file
10
Packages/Prettierd Format/LICENSE
Normal file
@@ -0,0 +1,10 @@
|
||||
Permission to use, copy, modify, and/or distribute this software for
|
||||
any purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
|
||||
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
||||
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
|
||||
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
||||
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
||||
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
25
Packages/Prettierd Format/Main.sublime-menu
Normal file
25
Packages/Prettierd Format/Main.sublime-menu
Normal file
@@ -0,0 +1,25 @@
|
||||
[
|
||||
{
|
||||
"id": "preferences",
|
||||
"children": [
|
||||
{
|
||||
"id": "package-settings",
|
||||
"children": [
|
||||
{
|
||||
"caption": "Prettierd Format",
|
||||
"children": [
|
||||
{
|
||||
"caption": "Settings",
|
||||
"command": "edit_settings",
|
||||
"args": {
|
||||
"base_file": "${packages}/Prettierd Format/prettierd_format.sublime-settings",
|
||||
"default": "// Settings in here override those in \"prettierd_format/prettierd_format.sublime-settings\"\n{\n\t$0\n}\n"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
91
Packages/Prettierd Format/README.md
Normal file
91
Packages/Prettierd Format/README.md
Normal file
@@ -0,0 +1,91 @@
|
||||
# Prettierd Format
|
||||
|
||||
Sublime Text plugin to format files faster using [prettierd](https://github.com/fsouza/prettierd).
|
||||
|
||||
<br />
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install [prettierd](https://github.com/fsouza/prettierd) globally with npm/yarn/pnpm:
|
||||
|
||||
```sh
|
||||
npm i -g @fsouza/prettierd
|
||||
```
|
||||
|
||||
2. Install this plugin with [Package Control](https://packagecontrol.io/packages/Prettierd%20Formatter):
|
||||
|
||||
`Package Control: Install Package` → `Prettierd Format`
|
||||
|
||||
3. Restart Sublime Text
|
||||
|
||||
<br />
|
||||
|
||||
## Usage
|
||||
|
||||
By default it formats on save any file supported by [Prettier](https://prettier.io/docs/en/) out-of-the-box.
|
||||
|
||||
### Commands
|
||||
|
||||
To format a file:
|
||||
|
||||
- `Prettierd: Format`
|
||||
|
||||
To save a file without formatting:
|
||||
|
||||
- `Prettierd: Save without formatting`
|
||||
|
||||
### Options
|
||||
|
||||
Enable/disable format on save:
|
||||
|
||||
```json
|
||||
"format_on_save": true
|
||||
```
|
||||
|
||||
Add additional extensions (enabled via prettier plugins) to be formatted either on save or commands:
|
||||
|
||||
```json
|
||||
"additional_extensions": ["php"]
|
||||
```
|
||||
|
||||
Exclude extensions from being formatted on save:
|
||||
|
||||
```json
|
||||
"disabled_extensions_on_save": ["md"]
|
||||
```
|
||||
|
||||
Exclude directories from being formatted on save:
|
||||
|
||||
```json
|
||||
"disabled_directories_on_save": ["*/node_modules/*"]
|
||||
```
|
||||
|
||||
Optional, path to `prettierd` executable. If not specified, it will be searched for in the system:
|
||||
|
||||
```json
|
||||
"prettierd_path": null
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
## Notes
|
||||
|
||||
This plugin does nothing else than piping the input to `prettierd` and replacing the view contents with the output.
|
||||
|
||||
It is basically just like executing `cat file.js | prettierd file.js` with the command line.
|
||||
|
||||
For this reason, any issue with prettier plugins or configuration should be investigated on the [prettierd](https://github.com/fsouza/prettierd) repo as it is the underlying tool actually interacting with Prettier.
|
||||
|
||||
### Astro / Svelte
|
||||
|
||||
As of September 2023, there are some upstream issues with Astro and Svelte files.
|
||||
|
||||
You can format them with the command `LSP: format` after installing their LSPs and enabling format on save for each language in the LSP settings.
|
||||
|
||||
Using the above command not only respects the `.prettierrc` configuration but is also very fast.
|
||||
|
||||
<br />
|
||||
|
||||
## License
|
||||
|
||||
0BSD
|
||||
3
Packages/Prettierd Format/messages.json
Normal file
3
Packages/Prettierd Format/messages.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"install": "messages/install.md"
|
||||
}
|
||||
24
Packages/Prettierd Format/messages/install.md
Normal file
24
Packages/Prettierd Format/messages/install.md
Normal file
@@ -0,0 +1,24 @@
|
||||
## Prettierd Setup
|
||||
|
||||
Readme: https://github.com/smastrom/sublime-prettierd-format
|
||||
|
||||
---
|
||||
|
||||
In order to start using this plugin, you must install prettierd globally, either using npm/yarn/pnpm:
|
||||
|
||||
```bash
|
||||
npm i -g @fsouza/prettierd
|
||||
```
|
||||
|
||||
or homebrew on macOS:
|
||||
|
||||
```bash
|
||||
brew install fsouza/prettierd/prettierd
|
||||
```
|
||||
|
||||
Then, format on save will be enabled by default for all files supported by Prettier out-of-the-box.
|
||||
|
||||
### Commands:
|
||||
|
||||
- Prettierd: Format
|
||||
- Prettierd: Save without formatting
|
||||
1
Packages/Prettierd Format/package-metadata.json
Normal file
1
Packages/Prettierd Format/package-metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"name": "Prettierd Format", "version": "0.6.0", "sublime_text": ">=3092", "platforms": ["*"], "python_version": "3.3", "url": "https://packagecontrol.io/packages/Prettierd%20Format", "issues": "https://github.com/smastrom/sublime-prettierd-format/issues", "author": ["Simone Mastromattei"], "description": "Sublime Text plugin to format files faster using prettierd", "labels": ["prettier", "prettierd", "format"], "libraries": [], "install_time": 1725486374.408112, "release_time": "2024-04-18 02:07:04"}
|
||||
5
Packages/Prettierd Format/prettierd_extensions.py
Normal file
5
Packages/Prettierd Format/prettierd_extensions.py
Normal file
@@ -0,0 +1,5 @@
|
||||
valid_extensions = [
|
||||
"cjs", "component.html", "css", "cts", "flow", "gql", "graphql", "hbs", "handlebars",
|
||||
"html", "js", "json", "json5", "jsx", "less", "markdown", "md", "mdx", "mjs", "mts",
|
||||
"pug", "scss", "ts", "tsx", "vue", "yaml", "yml"
|
||||
]
|
||||
41
Packages/Prettierd Format/prettierd_format.py
Normal file
41
Packages/Prettierd Format/prettierd_format.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
|
||||
from .prettierd_formatter import format_with_prettierd
|
||||
from .prettierd_extensions import valid_extensions
|
||||
|
||||
def get_settings():
|
||||
return sublime.load_settings("prettierd_format.sublime-settings")
|
||||
|
||||
class PrettierdFormatCommand(sublime_plugin.TextCommand):
|
||||
def run(self, edit):
|
||||
file_path = self.view.file_name()
|
||||
if not file_path:
|
||||
return
|
||||
|
||||
# Get file extension
|
||||
file_extension = file_path.split('.')[-1].lower()
|
||||
|
||||
# Fetch additional extensions from settings
|
||||
settings = get_settings()
|
||||
additional_extensions = settings.get("additional_extensions", [])
|
||||
all_extensions = valid_extensions + additional_extensions
|
||||
|
||||
if file_extension not in all_extensions:
|
||||
return
|
||||
|
||||
# If everything is okay, format the file
|
||||
print("Formatting file:", file_path)
|
||||
|
||||
current_content = self.view.substr(sublime.Region(0, self.view.size()))
|
||||
file_path = self.view.file_name()
|
||||
formatted_code = format_with_prettierd(current_content, file_path)
|
||||
|
||||
# print("Formatted Code:", formatted_code)
|
||||
|
||||
if formatted_code:
|
||||
self.view.run_command('replace_view_content', {'content': formatted_code})
|
||||
|
||||
class ReplaceViewContentCommand(sublime_plugin.TextCommand):
|
||||
def run(self, edit, content):
|
||||
self.view.replace(edit, sublime.Region(0, self.view.size()), content)
|
||||
18
Packages/Prettierd Format/prettierd_format.sublime-commands
Normal file
18
Packages/Prettierd Format/prettierd_format.sublime-commands
Normal file
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"caption": "Prettierd: Format",
|
||||
"command": "prettierd_format"
|
||||
},
|
||||
{
|
||||
"caption": "Prettierd: Save without formatting",
|
||||
"command": "prettierd_save_without_format"
|
||||
},
|
||||
{
|
||||
"caption": "Preferences: Prettierd Format",
|
||||
"command": "edit_settings",
|
||||
"args": {
|
||||
"base_file": "${packages}/Prettierd Format/prettierd_format.sublime-settings",
|
||||
"default": "// Settings in here override those in \"prettierd_format/prettierd_format.sublime-settings\"\n{\n\t$0\n}\n"
|
||||
}
|
||||
}
|
||||
]
|
||||
12
Packages/Prettierd Format/prettierd_format.sublime-settings
Normal file
12
Packages/Prettierd Format/prettierd_format.sublime-settings
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
// Whether to format on save.
|
||||
"format_on_save": true,
|
||||
// Any additional extension enabled via plugin that should be processed.
|
||||
"additional_extensions": [],
|
||||
// What extensions to disable when formatting on save.
|
||||
"disabled_extensions_on_save": [],
|
||||
// Directories to disable formatting on save.
|
||||
"disabled_directories_on_save": ["*/node_modules/*"],
|
||||
// Path to prettierd binary, optional.
|
||||
"prettierd_path": null
|
||||
}
|
||||
63
Packages/Prettierd Format/prettierd_format_and_save.py
Normal file
63
Packages/Prettierd Format/prettierd_format_and_save.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import subprocess
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
import os
|
||||
import re
|
||||
|
||||
from .prettierd_formatter import format_with_prettierd
|
||||
from .prettierd_extensions import valid_extensions
|
||||
|
||||
def get_settings():
|
||||
return sublime.load_settings("prettierd_format.sublime-settings")
|
||||
|
||||
class PrettierdFormatEventListener(sublime_plugin.EventListener):
|
||||
|
||||
def on_pre_save(self, view):
|
||||
settings = get_settings()
|
||||
format_on_save = settings.get("format_on_save", True)
|
||||
|
||||
if not format_on_save:
|
||||
return
|
||||
|
||||
# If skip_formatting is set on the view, reset it and exit early.
|
||||
if view.settings().get("skip_formatting", False):
|
||||
view.settings().erase("skip_formatting")
|
||||
return
|
||||
|
||||
file_path = view.file_name()
|
||||
if not file_path:
|
||||
return
|
||||
|
||||
file_extension = file_path.split('.')[-1].lower()
|
||||
|
||||
disabled_extensions_on_save = settings.get("disabled_extensions_on_save", [])
|
||||
disabled_directories_on_save = settings.get("disabled_directories_on_save", [])
|
||||
|
||||
additional_extensions = settings.get("additional_extensions", [])
|
||||
|
||||
if file_extension in disabled_extensions_on_save:
|
||||
return
|
||||
|
||||
# Add the plugin directory to disabled directories
|
||||
plugin_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
disabled_directories_on_save.append(plugin_dir)
|
||||
|
||||
# Check if the file belongs to a disabled directory
|
||||
for directory_pattern in disabled_directories_on_save:
|
||||
# Convert wildcard pattern to regex pattern
|
||||
regex_pattern = re.escape(directory_pattern).replace("\\*", ".*")
|
||||
if re.search(regex_pattern, file_path):
|
||||
return
|
||||
|
||||
# Check against all extensions
|
||||
all_extensions = valid_extensions + additional_extensions
|
||||
|
||||
if file_extension in all_extensions:
|
||||
current_content = view.substr(sublime.Region(0, view.size()))
|
||||
formatted_code = format_with_prettierd(current_content, file_path)
|
||||
if formatted_code:
|
||||
view.run_command('replace_view_content', {'content': formatted_code})
|
||||
|
||||
class ReplaceViewContentCommand(sublime_plugin.TextCommand):
|
||||
def run(self, edit, content):
|
||||
self.view.replace(edit, sublime.Region(0, self.view.size()), content)
|
||||
44
Packages/Prettierd Format/prettierd_formatter.py
Normal file
44
Packages/Prettierd Format/prettierd_formatter.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import sublime
|
||||
import shutil
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
def get_prettierd_path():
|
||||
settings = sublime.load_settings("Prettierd.sublime-settings")
|
||||
|
||||
settings_path = settings.get("prettierd_path", "")
|
||||
if settings_path:
|
||||
return settings_path
|
||||
|
||||
# Default behavior when not specified
|
||||
default_path = shutil.which("prettierd")
|
||||
if default_path:
|
||||
return default_path
|
||||
|
||||
sublime.error_message("prettierd executable not found.")
|
||||
return None
|
||||
|
||||
def format_with_prettierd(content, file_path):
|
||||
prettierd_path = get_prettierd_path()
|
||||
if not prettierd_path:
|
||||
message = "prettierd path not found."
|
||||
print(message)
|
||||
sublime.error_message(message)
|
||||
return None
|
||||
|
||||
cmd = [prettierd_path, "--stdin-filepath", file_path]
|
||||
|
||||
try:
|
||||
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.dirname(file_path))
|
||||
formatted_code, error = process.communicate(input=content.encode('utf-8'))
|
||||
except Exception as e:
|
||||
sublime.error_message("Failed to execute prettierd: " + str(e))
|
||||
return None
|
||||
|
||||
if process.returncode == 0:
|
||||
return formatted_code.decode('utf-8')
|
||||
else:
|
||||
error_message = error.decode('utf-8') if error.decode('utf-8') else "Unknown error"
|
||||
print(error_message)
|
||||
sublime.error_message("Error formatting the file with prettierd: " + error_message)
|
||||
return None
|
||||
@@ -0,0 +1,7 @@
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
|
||||
class PrettierdSaveWithoutFormatCommand(sublime_plugin.TextCommand):
|
||||
def run(self, edit):
|
||||
self.view.settings().set("skip_formatting", True)
|
||||
self.view.run_command("save")
|
||||
Reference in New Issue
Block a user