Skip to content

Commit f2f73e5

Browse files
authored
fix formatting (#152)
1 parent c1cc464 commit f2f73e5

File tree

4 files changed

+1365
-11
lines changed

4 files changed

+1365
-11
lines changed

src/main/kotlin/com/emberjs/gts/GtsSupport.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import com.intellij.psi.formatter.xml.XmlTagBlock
6767
import com.intellij.psi.html.HtmlTag
6868
import com.intellij.psi.impl.source.PsiFileImpl
6969
import com.intellij.psi.impl.source.html.HtmlDocumentImpl
70+
import com.intellij.psi.impl.source.html.HtmlFileImpl
7071
import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference
7172
import com.intellij.psi.impl.source.tree.LeafElement
7273
import com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
@@ -83,6 +84,7 @@ import com.intellij.refactoring.suggested.endOffset
8384
import com.intellij.refactoring.suggested.startOffset
8485
import com.intellij.util.Processor
8586
import com.intellij.xml.template.formatter.AbstractXmlTemplateFormattingModelBuilder
87+
import com.intellij.xml.template.formatter.TemplateFormatUtil
8688
import java.util.function.Predicate
8789
import javax.swing.Icon
8890

@@ -459,16 +461,32 @@ class GtsFileViewProviderFactory: FileViewProviderFactory {
459461

460462
}
461463

462-
class GtsFileViewProvider(manager: PsiManager, virtualFile: VirtualFile, eventSystemEnabled: Boolean, private val baseLang: GtsLanguage = GtsLanguage.INSTANCE) : MultiplePsiFilesPerDocumentFileViewProvider(manager, virtualFile, eventSystemEnabled), TemplateLanguageFileViewProvider {
464+
class GtsFileViewProvider(manager: PsiManager, virtualFile: VirtualFile, eventSystemEnabled: Boolean, public val baseLang: GtsLanguage = GtsLanguage.INSTANCE) : MultiplePsiFilesPerDocumentFileViewProvider(manager, virtualFile, eventSystemEnabled), TemplateLanguageFileViewProvider {
463465

464-
override fun findElementAt(offset: Int): PsiElement? {
465-
val element = super.findElementAt(offset)
466-
if (element.elementType == HbTokenTypes.CONTENT) {
467-
return super.findElementAt(offset, HTMLLanguage.INSTANCE)
466+
override fun findElementAt(offset: Int, language: Language): PsiElement? {
467+
var element: PsiElement?
468+
if (language == baseLang) {
469+
this.languages.forEach {
470+
element = super.findElementAt(offset, it)
471+
if (element !is OuterLanguageElement) {
472+
return element
473+
}
474+
}
468475
}
476+
element = super.findElementAt(offset, language)
469477
return element
470478
}
471479

480+
override fun findElementAt(offset: Int): PsiElement? {
481+
this.languages.forEach {
482+
val element = super.findElementAt(offset, it)
483+
if (element !is OuterLanguageElement) {
484+
return element
485+
}
486+
}
487+
return null
488+
}
489+
472490
override fun findReferenceAt(offset: Int): PsiReference? {
473491
val ref = super.findReferenceAt(offset)
474492
if (ref is PsiMultiReference) {
@@ -904,17 +922,20 @@ class RootBlockWrapper(val block: DataLanguageBlockWrapper, val policy: HtmlPoli
904922
}
905923

906924
fun getBaseIndent(forChild: Boolean = false): Indent? {
907-
val file = this.node!!.psi.containingFile.originalFile
925+
val viewProvider = this.node!!.psi.containingFile.viewProvider
926+
val htmlFile = viewProvider.getPsi(HTMLLanguage.INSTANCE)
927+
val jsFile = viewProvider.getPsi(JavaScriptSupportLoader.TYPESCRIPT) ?: viewProvider.getPsi(JavaScriptSupportLoader.ECMA_SCRIPT_6)
908928
val project = this.node!!.psi.project
909-
val document = PsiDocumentManager.getInstance(project).getDocument(file)!!
910-
val INDENT_SIZE = this.policy.settings.getIndentOptionsByDocument(project, document).INDENT_SIZE
929+
val document = PsiDocumentManager.getInstance(project).getDocument(htmlFile)!!
930+
val INDENT_SIZE = this.policy.settings.getIndentSize(htmlFile.language.associatedFileType)
931+
val JS_INDENT_SIZE = this.policy.settings.getIndentSize(jsFile.language.associatedFileType)
911932
if (this.parent != null) {
912933
val blockRef = this.parent as? JSAstBlockWrapper ?: ((this.parent as JsBlockWrapper).parent as JSAstBlockWrapper)
913934

914935
var startOffset: Int? = null
915936
if (blockRef.node!!.psi is JSClass) {
916937
val psiRef = blockRef.node!!.psi.parent
917-
startOffset = psiRef.textRange?.startOffset?.let { it + INDENT_SIZE }
938+
startOffset = psiRef.textRange?.startOffset?.let { it + JS_INDENT_SIZE }
918939
}
919940

920941
if (blockRef.node!!.psi.parent is JSVarStatement) {
@@ -924,7 +945,7 @@ class RootBlockWrapper(val block: DataLanguageBlockWrapper, val policy: HtmlPoli
924945
val lineTpl = document.getLineNumber(this.textRange.startOffset)
925946
val parentLine = document.getLineNumber(startOffset)
926947
if (lineTpl != parentLine) {
927-
startOffset += INDENT_SIZE
948+
startOffset += JS_INDENT_SIZE
928949
}
929950
}
930951
}
@@ -936,7 +957,7 @@ class RootBlockWrapper(val block: DataLanguageBlockWrapper, val policy: HtmlPoli
936957
val lineTpl = document.getLineNumber(this.textRange.startOffset)
937958
val parentLine = document.getLineNumber(startOffset)
938959
if (lineTpl != parentLine) {
939-
startOffset += INDENT_SIZE
960+
startOffset += JS_INDENT_SIZE
940961
}
941962
}
942963
}
@@ -1012,6 +1033,7 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
10121033
}
10131034
}
10141035

1036+
10151037
override fun createModel(formattingContext: FormattingContext): FormattingModel {
10161038

10171039
if (formattingContext.node is OuterLanguageElement) {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.emberjs.gts
2+
3+
import com.intellij.codeInsight.generation.CommentByBlockCommentHandler
4+
import com.intellij.openapi.actionSystem.IdeActions
5+
import com.intellij.openapi.application.ApplicationManager
6+
import com.intellij.openapi.command.CommandProcessor
7+
import com.intellij.openapi.editor.actionSystem.EditorActionManager
8+
import com.intellij.openapi.editor.actionSystem.TypedAction
9+
import com.intellij.openapi.editor.ex.EditorEx
10+
import com.intellij.openapi.project.Project
11+
import com.intellij.testFramework.PlatformTestUtil
12+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
13+
14+
abstract class GtsActionHandlerTest: BasePlatformTestCase() {
15+
private fun performWriteAction(project: Project, action: Runnable) {
16+
ApplicationManager.getApplication().runWriteAction {
17+
CommandProcessor.getInstance().executeCommand(project, action, "test command", null)
18+
}
19+
}
20+
21+
private fun validateTestStrings(before: String, expected: String) {
22+
require(
23+
!(!before.contains("<caret>")
24+
|| !expected.contains("<caret>"))
25+
) { "Test strings must contain \"<caret>\" to indicate caret position" }
26+
}
27+
28+
/**
29+
* Call this method to test behavior when the given charToType is typed at the &lt;caret&gt;.
30+
* See class documentation for more info: [HbActionHandlerTest]
31+
*/
32+
fun doCharTest(charToType: Char, before: String, expected: String) {
33+
EditorActionManager.getInstance()
34+
val typedAction = TypedAction.getInstance()
35+
doExecuteActionTest(
36+
before, expected
37+
) {
38+
typedAction.actionPerformed(
39+
myFixture.editor,
40+
charToType,
41+
(myFixture.editor as EditorEx).dataContext
42+
)
43+
}
44+
}
45+
46+
/**
47+
* Call this method to test behavior when Enter is typed.
48+
* See class documentation for more info: [HbActionHandlerTest]
49+
*/
50+
protected fun doEnterTest(before: String, expected: String) {
51+
val enterActionHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER)
52+
val gtsBefore = """<template>
53+
|${before.prependIndent(" ")}
54+
|</template>""".trimMargin()
55+
val gtsAfter = """<template>
56+
|${expected.prependIndent(" ")}
57+
|</template>""".trimMargin()
58+
doExecuteActionTest(
59+
gtsBefore, gtsAfter
60+
) {
61+
enterActionHandler.execute(
62+
myFixture.editor,
63+
null,
64+
(myFixture.editor as EditorEx).dataContext
65+
)
66+
}
67+
}
68+
69+
/**
70+
* Call this method to test behavior when the "Comment with Line Comment" action is executed.
71+
* See class documentation for more info: [HbActionHandlerTest]
72+
*/
73+
fun doLineCommentTest(before: String, expected: String) {
74+
doExecuteActionTest(
75+
before, expected
76+
) { PlatformTestUtil.invokeNamedAction(IdeActions.ACTION_COMMENT_LINE) }
77+
}
78+
79+
/**
80+
* Call this method to test behavior when the "Comment with Block Comment" action is executed.
81+
* See class documentation for more info: [HbActionHandlerTest]
82+
*/
83+
fun doBlockCommentTest(before: String, expected: String) {
84+
doExecuteActionTest(
85+
before, expected
86+
) {
87+
CommentByBlockCommentHandler().invoke(
88+
myFixture.project, myFixture.editor,
89+
myFixture.editor.caretModel.primaryCaret, myFixture.file
90+
)
91+
}
92+
}
93+
94+
private fun doExecuteActionTest(before: String, expected: String, action: Runnable) {
95+
validateTestStrings(before, expected)
96+
97+
myFixture.configureByText(GtsFileType.INSTANCE, before)
98+
performWriteAction(myFixture.project, action)
99+
myFixture.checkResult(expected)
100+
}
101+
}

0 commit comments

Comments
 (0)