Code Monkey home page Code Monkey logo

djlint-vscode's Introduction


djLint Logo

🏗️ Maintainers needed, please reach out on discord or email!

The missing formatter and linter for HTML templates.

tweet Discord Chat

Codecov Status Codacy Status Downloads npm Pypi Download

What lang are you using?

DjangoJinjaNunjucksTwigHandlebarsMustacheGoLangAngular

demo

🤔 For What?

Once upon a time all the other programming languages had a formatter and linter. Css, javascript, python, the c suite, typescript, ruby, php, go, swift, and you know the others. The cool kids on the block.

HTML templates were left out there on their own, in the cold, unformatted and unlinted :( The dirty corner in your repository. Something had to change.

djLint is a community build project to and add consistency to html templates.

✨ How?

Grab it with pip

pip install djlint

Or with the npm experimental install - Note, this requires python and pip to be on your system path.

npm i djlint

Lint your project

djlint . --extension=html.j2 --lint

Check your format

djlint . --extension=html.j2 --check

Fix my format!

djlint . --extension=html.j2 --reformat

💙 Like it?

Add a badge to your projects readme.md:

[![Code style: djlint](https://img.shields.io/badge/html%20style-djlint-blue.svg)](https://www.djlint.com)

Add a badge to your readme.rst:

.. image:: https://img.shields.io/badge/html%20style-djlint-blue.svg
   :target: https://www.djlint.com

Looks like this:

djLint

🛠️ Can I help?

Yes!

Would you like to add a rule to the linter? Take a look at the linter docs and source code

Are you a regex pro? Benchmark and submit a pr with improved regex for the linter rules

⚠️ Help Needed! ⚠️ Good with python? djLint was an experimental project and is catching on with other devs. Help out with a rewrite of the formatter to improve speed and html style for edge cases. Contribute on the 2.0 branch

🏃 Other Tools Of Note

  • DjHTML A pure-Python Django/Jinja template indenter without dependencies.
  • HTMLHint Static code analysis tool you need for your HTML
  • curlylint Experimental HTML templates linting for Jinja, Nunjucks, Django templates, Twig, Liquid

djlint-vscode's People

Contributors

christopherpickering avatar dependabot[bot] avatar monosans avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

djlint-vscode's Issues

Formatting is happening in the background

With all the other formatters I've used, I press a keyboard shortcut and the code cleans up on my screen. With this one:

  • The file is modified in the background.
  • I don't see any changes in my open file. I have to close it and open it again to see anything.
  • The "Formatting..." text doesn't show on the vscode status bar.

Is this how it is for everyone else or is something wrong on my end?

v2023.8.10 locks up keyboard in vscode.

Not quite sure what happens, but after upgrading to the latest version, writing in the editor stops working after a while. It looks as if it's the djlint command that gets stuck and blocks off something.

Downgrading to v2023.8.7 fixes things, I haven't tried .8 and .9.

VScode 1.81.1 on MacOS.

Extension not using venv djLint executable

The djLint executable was just updated with a bugfix, from v1.0.2 to v1.0.3. I updated it in my venv environment, but the fixed issue still occurs when the extension does the execution. I suppose it still uses the previous version – I could not find where the executed bin is located.

Some information to help you understand the problem:

  1. The djLint in my venv is at the correct version djlint, version 1.0.3.
  2. The version is correct when executing djLint manually in my venv.
  3. The Use Venv option is set as true in the extension configuration.

I resolved the issue with the extension by running the Reinstall djLint option in VSCode – but I think I should not have to do that if I updated it manually and Use Venv is checked.

If this behavior is normal, could it be explained in the readme?

settings djlint.configuration relative paths

I have workspace open on a project folder e.g. /home/foo/projectA/
I'm customizing djlint with my own config file in project folder /home/foo/projectA/.djlintrc
I'm having template files in various subdirs e.g. ./templates/main.html, ./myapp/templates/page.html

Let me elaborate on various options in settings.json

    // all works ok, but I don't like the absolute path
    "djlint.configuration": "/home/foo/projectA/.djlintrc"
    // relative path in project doesn't work - Error: Invalid value for '--configuration': Path '.djlintrc' does not exist.
    "djlint.configuration": ".djlintrc"
    // homedir expantion doesn't work - Error: Invalid value for '--configuration': Path '.djlintrc' does not exist.
    "djlint.configuration": "~/projectA/.djlintrc"
    // if I put relative path from the file I'm linting, it works

    // works for file `./templates/main.html`
    "djlint.configuration": "../.djlintrc"

    // works for file `./myapp/templates/pagehtml`
    "djlint.configuration": "../../.djlintrc"

It feels quite tricky to set this correctly, which seems to be absolute path for me (dislike).

It seems that cwd for djlint execution is path of a target file. For finding config file it may be beneficial to execute djlint from workspace base dir.

Ideally, would it be possible to pickup .djlintrc file in workspace base folder automatically by default?

BTW: docs say:

djLint’s CLI options can be configured directly in the VS Code settings.

But I'm only able to pass djlint.configuration argument, is it possible to pass for example max_line_length or such?

P.S.: Thanks for this extension, 💚!

Should use vscode's indent spacing

Linting a django-html file with the indentation set to 2 spaces ends up with the standard 4-spaced indentation.

From reading the vscode api, it looks like the provideDocumentFormattingEdits could accept a FormattingOptions object as the second arg which has a tabSize property. Then you can use that to populate the --indent arg rather than the hard coded (and probably redundant) djlint.indent configuration setting.

Issue with overwriting files since 1.0.6 caused by problem with gitignore parsing

Hi!

I just noticed today that formatting any django html file with the djlint extension results in the file getting overwritten with "No files to check! 😢", on version 1.0.7.

If I roll back to 1.0.6 this still happens, but on 1.0.5 it doesn't.

I've reinstalled vscode and removed global settings, as well as resetting any workspace-specific settings.

After a bit of investigating, I found that it's caused, somehow, by the project's gitignore file - it was ignoring folders named var/, and that causes the extension to not find any files anymore. To be clear, it's unrelated to the filename - any file in any folder within the project causes the error.

Note that this doesn't seem to be caused by djlint itself - if i manually run djlint --use-gitignore <file> it works correctly, even with the var/ in the gitignore, only ignoring the file and producing the message if the path to the file is actually included in the gitignore.

To reproduce

  • Create a new workspace
  • Add a pyproject.toml with [tool.djlint] and use_gitignore = true
  • Add a .gitignore with just var/
  • Try to format any file, contents get replaced with the no files to check message

ALT+Shift+F does not format the Django HTML file

I added the following adjustments in the settings.json file and the extension does not do anything, the command ALT+Shift+F also is not working.

"[html]": {
"editor.defaultFormatter": "monosans.djlint"
},
"[django-html]": {
"editor.defaultFormatter": "monosans.djlint"
},

Publish to Open VSX Registry

Thanks for the fantastic project, it makes my Django templates look first-rate! Would the project be willing to publish this extension to the Open VSX Registry? Open source projects that are based on vs-code (like vscodium) are not allowed use the Microsoft Extension Marketplace, hence the Open VSX registry was created and now has over 2000 extensions available. There are instructions on how to publish, and the directions make it look relatively pretty simple. Thank you!

Formatter is appending to file

This just started happening today after months of using this plugin. Whenever I save a file, the entire contents of that file are formatted then appended to the end, with messed up formatting in the beginning. For example:

Before

<h1>Heading</h1>
<form method="post" action="{% url 'a_url' %}" class="a_class">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit" name="action">Submit</button>
</form>

After

<h1>
Heading
</h1>
<form method="post" action="{% url 'a_url' %}" class="a_class">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit" name="action">
Submit
</button>
</form>

<h1>Heading</h1>
<form method="post" action="{% url 'a_url' %}" class="a_class">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit" name="action">Submit</button>
</form>

From what I can tell, there have been no updates or environment changes in the past day.

Environment

Version: 1.70.2 (user setup)
Commit: e4503b30fc78200f846c62cf8091b76ff5547662
Date: 2022-08-16T05:35:13.448Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Windows_NT x64 10.0.19044

Non latin symbols replaced with `????` after formatting in Windows 10.

If template contains non ASCII symbols after formatting they're replaced with ? characters. Running djLint directly from command line does not have this issue. So I guess it's somewhere in extension. File encoding is utf-8, OS is Windows 10. In Linux and MacOS everything works as expected.

Indenting broken

I am working through the MDN Django Tutorial and noticed this problem:

{% extends "base_generic.django-html" %}
{% block content %}
    <h1>Title: {{ book.title }}</h1>
    <p>
        <strong>Author:</strong><a href="">{{ book.author }}</a>
    </p>
    <p>
        <strong>Summary:</strong> <span>{{ book.summary }}</span>
    </p>
    <p>
        <strong>ISBN:</strong> <span>{{ book.isbn }}</span>
    </p>
{% endblock content %}
{% block content %}
    <h1>Title: {{ book.title }}</h1>
    <p>
        <strong>Author:</strong><a href="">{{ book.author }}</a>
    </p>
    <p>
        {{ book.summary }}
    </p>
    <p>
        {{ book.isbn }}
    </p>
{% endblock content %}
{% block content %}
    <h1>Title: {{ book.title }}</h1>
    <p>
        <strong>Author:</strong><a href="">{{ book.author }}</a>
    </p>
    <p>
        <strong>Summary:</strong> {{ book.summary }}
</p>
<p>
    <strong>ISBN:</strong> {{ book.isbn }}
</p>
{% endblock content %}

I have repeated the content block 3 times here to demonstrate the issue.
The final content block shows the issue. The p tag indentation breaks when they contain a strong tag followed by a variable.
The top block shows that it works OK if I enclose the variable in a tags or span tags ( I assume any tags would work ).
The middle block shows that it works if the p tags only contain a variable.

DJLint is the only formatter installed, but I also have the Baptiste Darthenay Django extension installed. My settings.json looks like this:

{
    "workbench.colorTheme": "Default Dark+",
    "emmet.includeLanguages": {
        "django-html": "html"
    },
    "files.associations": {
        "*.django-html": "DjangoHTML"
    },
    "[django-html]": {
        "editor.defaultFormatter": "monosans.djlint"
    },
    "editor.minimap.enabled": false,
}

Support .hbs extension

djlint also supports handlebars files, would be great to add support to the vscode extension.

No ignore option

There is no ignore option for djlint

-i, --ignore TEXT Codes to ignore. ex: "H014,H017"

It is better in setting.json

like this

{
    "djlint.ignore": "H014,H017",
}

or

{
    "djlint.ignore": ["H014", "H017"],
}

This breaks when formatting a django template with Japanese characters in UTF-8.

This breaks when formatting a django template with Japanese characters in UTF-8.

Before formatting
image

After formatting
image

Is it possible to increase the option to explicitly specify the character code in settings.json?

Below is my environment.

  • OS - Windows 10 Japanese
  • File encoding - UTF-8 CRLF
  • Python 3.9.10
  • djLint version - 1.0.2
  • djlint-vscode version - 1.1.0

Extension tries to access clipboard

I am using this excellent extension in VS Code on Gitpod (Cloud IDE). Unfortunately, when the extension is enabled and I edit the templates, I always get the message that it tries to access the clipboard (entire message below). As soon as I disable this extension, this message no longer pops up. Does this extension really need access to the clipboard? (Quite problematic as this is not fully supported in Firefox.)

Unable to read from the browser's clipboard. Please make sure you have granted access for this website to read from the clipboard.

Regular ENOENT when using in SSH Remote

When using your extension I am getting regular those errors:

2023-07-20 14:10:51.956 [error] Error: Command failed with ENOENT: /var/www/html/DIRECTORY.I.USE/.venv/bin/python -m djlint - --quiet --linter-output-format <filename>{filename}</filename><line>{line}</line><code>{code}</code><message>{message}</message>
spawn /var/www/html/DIRECTORY.I.USE/.venv/bin/python ENOENT
	at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
	at onErrorNT (node:internal/child_process:478:16)
	at processTicksAndRejections (node:internal/process/task_queues:83:21)

Wrong HTML formatting?

I'm just started learning Django and HTML. Got HTML file to work with from my course and tried to format it.

Sorry for long code, but don't want to modify it specially for the issue.

Code before formatting:
before.txt
And after:
after.txt

As you can see, the end of the formatted file going too far from the start of the line. Tried Beautify extension, and it formatted as I would expect.
Am I missing something? Maybe it should be better configured?

Configuration from pyproject.toml is being ignore in v2022.10.1

After updating to v2022.10.1, extension started displaying problems in base.html that I was ignoring in config file.
Consider adding a meta description. <html lang="en"> (H030)
Consider adding meta keywords. <html lang="en"> (H031)

My project has the pyproject.toml at root level with config values for djliint:

[tool.djlint] 
ignore = "H005,H030,H031,H021,T002,D018" 
profile = "django" 
use_gitignore = true 

I'm using Poetry venv 1.1.13 with djlint v1.7.0 and extension v2022.10.1.
I tried updating djlint to the latest version v1.19.1 but no luck fixing the problem.

Code duplicated when formatting Django HTML

👋 Hi, something strange is happening.
When I use the extension to format Django HTML code the content of the file is duplicated.

I have updated DjLint and the extension.
I had to disable the automatic format and when I wanted to format I have to delete all the code generated by the duplication.

Is this a known bug?

Thank you!

Formatter not working when pyproject.toml not at project root

My project dir structure is like this:

/project
/project/django
/project/django/pyproject.toml
/project/django/app/template/blop.html

The djlint config is inside pyproject.toml :

[tool.djlint]
profile="django"
max_line_length=120
indent=2
blank_line_after_tag="load,extends,include"

The blop.html file language mode is django-html

When running djlint from command line it work as expected:
[/absolute/path/to/project/]$ djlint --reformat django/app/template/blop.html

But when running the formatter from vscode it seems to apply a default configuration (blank_line_after_tag is not respected)

I'v tried to force djlint configuration in vscode config but it still doesn't work:

{
  "djlint.useEditorIndentation": false,
  "djlint.configuration": "/absolute/path/to/project/django/pyproject.toml"
}

The only way I've found so far to have it working is to put pyproject.toml at project root. But it is not a nice solution for me.

Other notes:

  • There is a python venv. Workspace is configured to use it.
  • djlint 1.19.0 is installed inside the venv

Did I miss something ? How to have djlint-vscode respect my pyproject.toml file ?
By the way there is a way to have logs of what djlint-vscode is doing ?

djLint is not installed for the current active Python interpreter.

I have installed djlint globally using pip in my macOS Ventura on my M1 Air. I have also set up the configuration in Visual Studio Code's settings. However, whenever I save a Django HTML file that contains Django templates, I receive the following error message: "djLint is not installed for the current active Python interpreter."

Steps to Reproduce:

  1. Install djlint globally using pip.
  2. Configure Visual Studio Code to use djlint in the settings.
  3. Save a Django HTML file with Django templates.

Expected Behavior:

  • The Django HTML file should be successfully linted using djlint without any errors.

Actual Behavior:

  • Upon saving the Django HTML file, the error message "djLint is not installed for the current active Python interpreter" is displayed.

Additional Information:

  • I am running macOS Ventura on an M1 Air.
  • I have verified that djlint is installed globally using pip.
  • I have followed the instructions to configure Visual Studio Code to use djlint.
  • Other Python-related functionalities in Visual Studio Code are working correctly.

Please let me know if there are any specific steps or configurations I might be missing to resolve this issue. Thank you!

Request: Include autocomplete functionality

I know nothing about coding vscode extensions, but I rely on autocomplete functions on the HTML formatter. Is it possible to have this extension include the same functionality? I can't use it without.

I mainly type div.col-6 , etc...

Or maybe there's a way where I can just adjust my settings for this? Thanks :)

Is it possible to autofix certain problems?

Hello! Thanks for this beautiful extension :)

I was wondering if there's a way to enable auto fixing of certain problems like for example the two mentioned below?
It does work for some problems (like adding spaces inside a variable), but it'd be really nice if it could work with these too.

Example

Given this code:

{% block name %}
   {% include 'header.twig' %}
{% endblock %}

These problems are reported in the Problems panel, based on my .djlintrc config:

  • Endblock should have name. Ex: {% endblock body %}. {% endblock %} (T003)
  • Double quotes should be used in tags. {% include 'header.t (T002)

And ideally the code above should be replaced with the following on save:

{% block name %}
   {% include "header.twig" %}
              ^           ^
{% endblock name %}
            ^^^^

Or perhaps this is something I should request in the Djlint repo?

Thank you! ❤️

Support pipx-installed djlint

Thanks for this nice extension!

Typically, I prefer installing general Python tools not in every venv separately but to install them with pipx globally. However, djlint-vscode does not execute djlint that is installed by pipx and is on path. If I understand the code correctly, djlint-vscode always executes djlint via python -m djlint.

Would it be possible to run djlint directly, e.g. if djlint.useVenv = false and djlint.pythonPath is not set?

Is it possible to disable the extension for specific file types?

I like to use the extension when working on jinja within a python environment but when working on nunjucks I don't want the extension to do anything, but every time I save a file I get "djLint is not installed for the current active Python interpreter."

Can I disable the extension for certain file types, or maybe if there is no config file?

djLint is not installed for the current Python interpreter.

Hi!

I get the message djLint is not installed for the current Python interpreter.

I use a virtual environment and I have djLint in this environment. In the VS Code terminal (which uses the default environment) djlint works fine.

Any idea how I can fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.