Auto-Formatting does indentation on thing that it should not indent

已回答

Hello everyone,

I am currently creating a plugin to support Eiffel language in Intellij Idea and I am stuck with a problem in the formatting model. To show the problem, I created a branch in my repo here: https://gitlab.com/tioui/intelleiffel/-/tree/formatting_bug?ref_type=heads

Here the description of the problem. There is wrong auto-formatting that is done. I did not program those formatting (well maybe I did and I don't know that).

I use a basic formatting block that does not indent anything (this is a minimal example of course) : https://gitlab.com/tioui/intelleiffel/-/blob/formatting_bug/src/main/java/net/libreti/intelleiffel/formatter/EiffelBlock.java?ref_type=heads

The FormattingModelBuilder is also minimal: https://gitlab.com/tioui/intelleiffel/-/blob/formatting_bug/src/main/java/net/libreti/intelleiffel/formatter/EiffelFormattingModelBuilder.java?ref_type=heads

Then, I use the formatter with this Eiffel code:

test     (an_argument:STRING)
         -- Documentation
         do
                if an_argument ~ "Hello" then
      print("Hello")
end
             end

Normally, it should give something like that, since it should not indent anything:

test     (an_argument:STRING)
-- Documentation
do
if an_argument ~ "Hello" then
print("Hello")
end
end

But the result I have it like that:

test     (an_argument:STRING)
         -- Documentation
         do
         if an_argument ~ "Hello" then
            print("Hello")
         end
         end

It seems to try to follow the arguments parenthesis for an obscure reason. It also correctly (or magically) manage the body of the if conditional.

If anyone can give me a hint about what could go wrong; I would really appreciate it.

Note that the minimal Eiffel code is there: https://gitlab.com/tioui/intelleiffel/-/blob/formatting_bug/test_eiffel/class_a.e?ref_type=heads

0
正式评论

I know it's quite extensive information, but please read this section again. The “indent” and “alignment” topics are especially important to you. That being said, in your formatter, you use normalIndent() and create alignments for blocks. This is what creates the alignment of your code under the block above and also the indent of print("Hello").

I debugged your plugin. If you check these lines in your code and replace all alignments with null and the normal indents with Indent.getNoneIndent(), you will get the desired result. And as a side comment, keep working (and struggling) through it. Understanding and writing a good formatter is hard, but you will get there.

I assume you have already tried to debug this using https://plugins.jetbrains.com/docs/intellij/code-formatting.html#introduction to see the runtime structure?

1

Indeed I did. All Indent seems to be NONE and all Align seems to be a different one

For the routine indentation (the one that is align the parenthesis):

And for the if body:

0

Thanks. Changing all the Alignment.createAlignment() to null fix the problem.

0

请先登录再写评论。