bitst0rm-pub / Formatter

🧜‍♀️ A Sublime Text plugin to beautify, minify, convert code: CSS, SCSS, Sass, HTML, XML, SVG,JS,JavaScript, JSON, GraphQL, Markdown, TypeScript, Vue, Lua, YAML, Go, Perl, PHP, Python, Ruby, Rust, Haskell, Dart, Swift, Crystal, Bash, Shell, SQL, CSV, C, C++, C#, Objective-C, D, Java, Pawn, Julia, Blade, LaTeX, D2, Graphviz, Mermaid, PlantUML, etc
Other
105 stars 22 forks source link

Resolve Local Prettier #36

Closed wkirby closed 1 year ago

wkirby commented 1 year ago

It's very common to have multiple JS/TS projects, each with a different version of prettier installed. In these cases, it's not particularly useful to have prettier installed globally, and there's no one executable_path that can be set to resolve the correct prettier executable for each project.

This adapts some code from https://github.com/jonlabelle/SublimeJsPrettier to recursively search for a local install of prettier only if there is no global prettier executable and executable_path is not set.

This search is naive, in that it takes the currently open view, and looks in each directory for known node_modules paths for prettier until it finds one or reaches the os root directory.

bitst0rm commented 1 year ago

Thanks for the commit. I haven't tested your patch yet; just looking at the code. It appears that JsPrettier only works with saved/existing files, while Formatter handles both saved files and unsaved buffers/view. This is where we encounter a problem at the first line of the logic: active_view_parents = make_parent_directories(os.path.dirname(self.view.file_name()), limit=500) We will get error here for dirty file, that has neither a filename nor a parent directory on the disk. It is ok for existing files with this patch.

bitst0rm commented 1 year ago

@wkirby branch master should fix the issue, both for saved and unsaved file. In your User Settings you should add a default valid executable_path for the case that local exec not found.

closed as fixed.

wkirby commented 1 year ago

@bitst0rm thanks! Appreciate your feedback and integration.