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

Indentation in non-HTML documents #91

Open
gdude2002 opened this issue Mar 22, 2021 · 7 comments · May be fixed by #162
Open

Indentation in non-HTML documents #91

gdude2002 opened this issue Mar 22, 2021 · 7 comments · May be fixed by #162
Assignees
Labels

Comments

@gdude2002
Copy link

gdude2002 commented Mar 22, 2021

I'm currently working on a project that makes use of a custom SSG (static site generator) to generate a website from Markdown and HTML files, all of which may be Pebble templates. When the directory containing the site's sources is set up via IJ's template language settings as containing Markdown-formatted Pebble templates, and the files end in .md.peb, the way indentation is handled in the files is… strange, to say the least.

As an example, in a document with no Pebble syntax whatsoever, let's create a partial sentence on a single line.

image

Hitting enter at the end of this line places the caret on the next line, indented to a depth of one.

image

Pressing enter again places the caret at on the next line, but without an indent, leaving the indent present on the previous line.

image

This is frustrating to work with as, when writing documents in a format such as Markdown, I constantly have to dedent lines.

Any ideas on this one?

@grishka
Copy link

grishka commented Jul 10, 2021

Even with HTML, it's waaaay too pushy with doing indentation its own, bizarre way. You have to select and delete these tabs manually, or use left arrow and then delete, because just pressing backspace deletes all the indents and the preceding line break.

I wish there was a setting to disable automatic indentation altogether, because in its current form it's actively unhelpful and frustrating in 95% of the cases.

@Martmists-GH
Copy link

For anyone still having this issue, here's a build of the plugin which does no indenting except where the templated language indicates it should indent.
pebble-intellij.zip

@cosmin-marginean
Copy link

@Martmists-GH Are you able to share the code change required here? I'd be happy to provide a PR for this.
Thanks!

@Martmists-GH
Copy link

Martmists-GH commented Apr 4, 2023

EDIT: Decided to rewrite with an attempt at making indents work properly; If you need the version without indents, use the bottom snippet. I'll make a PR for this later today.

<removed for being bad code, rewriting...>

I think a PR would not work as it completely disables all pebble indents and only uses those from the data language, but here you go:

// File: src/main/kotlin/com/github/bjansen/intellij/pebble/formatting/PebbleFormattingModelBuilder.kt
class PebbleBlock(
    node: ASTNode,
    wrap: Wrap?,
    alignment: Alignment?,
    settings: CodeStyleSettings,
    blockFactory: TemplateLanguageBlockFactory,
    foreignChildren: MutableList<DataLanguageBlockWrapper>?
) : TemplateLanguageBlock(node, wrap, alignment, blockFactory, settings, foreignChildren) {

    override fun getTemplateTextElementType() = tokens[PebbleLexer.CONTENT]

    override fun getIndent(): Indent? {
        // ignore whitespace
        if (myNode.text.trim().isEmpty()) {
            return Indent.getNoneIndent()
        }

        val parent = parent

        if (parent is DataLanguageBlockWrapper) {
            return Indent.getNoneIndent()  // Modified
        } else if (parent is PebbleBlock) {
            if (parent.node.lastChildNode == node) {
                // we're the parent's end tag, don't indent
                return Indent.getNoneIndent()
            }

            for (subBlock in parent.subBlocks) {
                if (subBlock is DataLanguageBlockWrapper && subBlock.textRange.startOffset < textRange.startOffset) {
                    // We're next to a data language block, so we use its indent
                    return subBlock.indent
                }
            }

            if (node.psi is PebbleTagDirective) {
                // we're an opening tag, indent
                return Indent.getNoneIndent()  // Modified
            }
        }

        return Indent.getNoneIndent()
    }

    override fun getChildIndent(): Indent? {
        return Indent.getNoneIndent()  // Modified
    }
}

@cosmin-marginean
Copy link

Ah, thank you so much @Martmists-GH!

@grishka
Copy link

grishka commented Apr 4, 2023

I installed @Martmists-GH's version and it's just so. much. better. It's so satisfying to not be frustrated.

For something pull-request-worthy I'd suggest adding a checkbox that selects between old and new behavior to this settings UI the plugin already has:
Снимок экрана 2023-04-04 в 19 32 34

@Martmists-GH Martmists-GH linked a pull request Apr 4, 2023 that will close this issue
@bjansen
Copy link
Owner

bjansen commented Apr 5, 2023

@Martmists-GH thanks for the PR, I'm reviewing it!

@bjansen bjansen added the t-bug label Apr 5, 2023
@bjansen bjansen self-assigned this Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants