# JetFormattingModelBuilder

**URL:** <https://discuss.kotlinlang.org/t/jetformattingmodelbuilder/1307>\
**Category:** Uncategorized\
**Created:** [May 24, 2013, 7:42am UTC](https://discuss.kotlinlang.org/t/jetformattingmodelbuilder/1307 "2013-05-24T07:42:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Factitious](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/factitious/32/1550_2.png) [@Factitious](https://discuss.kotlinlang.org/u/Factitious)\
**Post date:** [May 24, 2013, 7:42am UTC](https://discuss.kotlinlang.org/t/jetformattingmodelbuilder/1307/1 "2013-05-24T07:42:29Z")

</div>

Why in JetFormattingModelBuilder.createModel(element, settings) model created around given element, but not a PsiFile a whole?

E.g. why we now have:

kotlin/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java

&nbsp;&nbsp;public FormattingModel createModel(PsiElement **element** , CodeStyleSettings settings) {  
 &nbsp;&nbsp;JetBlock block = new JetBlock(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; **element**.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;createSpacingBuilder(settings));  
  
   
 &nbsp;&nbsp;return FormattingModelProvider.createFormattingModelForPsiFile(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;element.getContainingFile(), block, settings);  
 &nbsp;&nbsp;}

instead of (my exception, based on [http://devnet.jetbrains.com/message/5216859#5216859](http://devnet.jetbrains.com/message/5216859#5216859)  
**Q:** And what Block do i need to pass to this FormattingModelProvider.createFormattingModelForPsiFile  
**A(Dmitry Jemerov****):** The block describing your entire file (with sub-blocks for AST nodes contained  
in the file).  
):

&nbsp;&nbsp;PsiFile **containingFile** = **element**.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE);  
 &nbsp;&nbsp;JetBlock block = new JetBlock(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; **containingFile**.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;createSpacingBuilder(settings));  
  
   
 &nbsp;&nbsp;return FormattingModelProvider.createFormattingModelForPsiFile(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;element.getContainingFile(), block, settings);

It is bug?

If so, similar bug in tutorial here [http://confluence.jetbrains.com/display/IntelliJIDEA/Formatter](http://confluence.jetbrains.com/display/IntelliJIDEA/Formatter)

P.S. For Java we have:

/com/intellij/lang/java/JavaFormattingModelBuilder.class

&nbsp;&nbsp;public FormattingModel createModel(final PsiElement **element** , final CodeStyleSettings settings) {  
 &nbsp;&nbsp;final FileElement **fileElement** = TreeUtil.getFileElement((TreeElement)SourceTreeToPsiMap.psiElementToTree( **element** ));  
 &nbsp;&nbsp;LOG.assertTrue(fileElement != null, "File element should not be null for " + element);  
 &nbsp;&nbsp;CommonCodeStyleSettings javaSettings = settings.getCommonSettings(JavaLanguage.INSTANCE);  
 &nbsp;&nbsp;Block block = AbstractJavaBlock.createJavaBlock( **fileElement** , javaSettings);  
 &nbsp;&nbsp;FormattingDocumentModelImpl model = FormattingDocumentModelImpl.createOn(element.getContainingFile());  
 &nbsp;&nbsp;return new PsiBasedFormatterModelWithShiftIndentInside (element.getContainingFile(), block, model);  
&nbsp;&nbsp;}

P.P.S. I’m implemeting KDoc lexer, parser & formatter (KT-1545, KT-3161 and other…) and got error: Intelij trying to reformat KDoc comment and says that function above is not covered by blocks.

---

<div class="post-metadata">

**Author:** ![Factitious](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/factitious/32/1550_2.png) [@Factitious](https://discuss.kotlinlang.org/u/Factitious)\
**Post date:** [May 24, 2013, 9:17am UTC](https://discuss.kotlinlang.org/t/jetformattingmodelbuilder/1307/2 "2013-05-24T09:17:57Z")

</div>

see [http://devnet.jetbrains.com/message/5488287#5488287](http://devnet.jetbrains.com/message/5488287#5488287)

> I consider that to be a custom language tutorial bug. Even [FormattingModelProvider.](https://github.com/JetBrains/intellij-community/blob/master/platform/lang-api/src/com/intellij/formatting/FormattingModelProvider.java#L41)[createFormattingModelForPsiFile() javadoc](https://github.com/JetBrains/intellij-community/blob/master/platform/lang-api/src/com/intellij/formatting/FormattingModelProvider.java#L41) says that root block should be providedI've created corresponding ticket - [IDEA-107927](http://youtrack.jetbrains.com/issue/IDEA-107927).
> 
> Denis

---

<div class="post-metadata">

**Author:** ![Factitious](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/factitious/32/1550_2.png) [@Factitious](https://discuss.kotlinlang.org/u/Factitious)\
**Post date:** [May 24, 2013, 11:19am UTC](https://discuss.kotlinlang.org/t/jetformattingmodelbuilder/1307/3 "2013-05-24T11:19:02Z")

</div>

I'm changed JetFormattingModelBuilder.createModel in [https://github.com/JetBrains/kotlin/pull/280](https://github.com/JetBrains/kotlin/pull/280)
