Skip to content

Commit

Permalink
bug fix : code style settings #138 #141
Browse files Browse the repository at this point in the history
  • Loading branch information
tangzx committed Apr 28, 2018
1 parent 2d1423e commit acd497f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.tang.intellij.lua.lang.LuaLanguage
*/
class LuaCodeStyleSettingsProvider : CodeStyleSettingsProvider() {
override fun createSettingsPage(settings: CodeStyleSettings, originalSettings: CodeStyleSettings): Configurable {
return object : CodeStyleAbstractConfigurable(settings, originalSettings, "Lua") {
return object : CodeStyleAbstractConfigurable(settings, originalSettings, "EmmyLua") {

override fun getHelpTopic() = "reference.settingsdialog.codestyle.lua"

Expand All @@ -51,7 +51,7 @@ class LuaCodeStyleSettingsProvider : CodeStyleSettingsProvider() {
}
}

override fun getConfigurableDisplayName() = LuaLanguage.INSTANCE.id
override fun getConfigurableDisplayName() = LuaLanguage.INSTANCE.displayName

override fun createCustomSettings(settings: CodeStyleSettings?): CustomCodeStyleSettings? {
return LuaCodeStyleSettings(settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package com.tang.intellij.lua.editor.formatter

import com.intellij.formatting.Alignment
import com.intellij.formatting.SpacingBuilder
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.tang.intellij.lua.editor.formatter.blocks.LuaScriptBlock

class LuaFormatContext(val settings: CodeStyleSettings, val luaSettings: LuaCodeStyleSettings, val spaceBuilder: SpacingBuilder) {
class LuaFormatContext(val settings: CommonCodeStyleSettings, val luaSettings: LuaCodeStyleSettings, val spaceBuilder: SpacingBuilder) {
var eqAlignment: Alignment? = null
var lastBlock: LuaScriptBlock? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import com.tang.intellij.lua.psi.LuaTypes.*
*/
class LuaFormattingModelBuilder : FormattingModelBuilder {
override fun createModel(element: PsiElement, settings: CodeStyleSettings): FormattingModel {
val ctx = LuaFormatContext(settings, settings.getCustomSettings(LuaCodeStyleSettings::class.java), createSpaceBuilder(settings))
val commonSettings = settings.getCommonSettings(LuaLanguage.INSTANCE)
val luaSettings = settings.getCustomSettings(LuaCodeStyleSettings::class.java)
val ctx = LuaFormatContext(commonSettings, luaSettings, createSpaceBuilder(settings))
return FormattingModelProvider.createFormattingModelForPsiFile(element.containingFile,
LuaScriptBlock(element,
Wrap.createWrap(WrapType.NONE, false),
Expand All @@ -46,6 +48,7 @@ class LuaFormattingModelBuilder : FormattingModelBuilder {

private fun createSpaceBuilder(settings: CodeStyleSettings): SpacingBuilder {
val luaCodeStyleSettings = settings.getCustomSettings(LuaCodeStyleSettings::class.java)
val commonSettings = settings.getCommonSettings(LuaLanguage.INSTANCE)

return SpacingBuilder(settings, LuaLanguage.INSTANCE)
.before(END).lineBreakInCode()
Expand All @@ -54,8 +57,8 @@ class LuaFormattingModelBuilder : FormattingModelBuilder {
.around(ELSE).lineBreakInCode()
.before(ELSEIF).lineBreakInCode()
.after(LOCAL).spaces(1) //local<SPACE>
.before(COMMA).spaces(if (settings.SPACE_BEFORE_COMMA) 1 else 0)
.after(COMMA).spaces(if (settings.SPACE_AFTER_COMMA) 1 else 0) //,<SPACE>
.before(COMMA).spaces(if (commonSettings.SPACE_BEFORE_COMMA) 1 else 0)
.after(COMMA).spaces(if (commonSettings.SPACE_AFTER_COMMA) 1 else 0) //,<SPACE>
.between(LCURLY, TABLE_FIELD).spaces(1) // {<SPACE>1, 2 }
.between(TABLE_FIELD, RCURLY).spaces(1) // { 1, 2<SPACE>}
.before(TABLE_FIELD_SEP).none() // { 1<SPACE>, 2 }
Expand All @@ -64,9 +67,9 @@ class LuaFormattingModelBuilder : FormattingModelBuilder {
.afterInside(RPAREN, FUNC_BODY).lineBreakInCode()
.between(FUNCTION, FUNC_BODY).none()
.between(FUNCTION, NAME_DEF).spaces(1) //function<SPACE>name()
.around(BINARY_OP).spaces(if (settings.SPACE_AROUND_ASSIGNMENT_OPERATORS) 1 else 0)
.around(BINARY_OP).spaces(if (commonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) 1 else 0)
.around(UNARY_OP).none()
.around(ASSIGN).lineBreakOrForceSpace(false, settings.SPACE_AROUND_ASSIGNMENT_OPERATORS) // = 号两头不能换行
.around(ASSIGN).lineBreakOrForceSpace(false, commonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) // = 号两头不能换行
.around(LuaSyntaxHighlighter.KEYWORD_TOKENS).spaces(1)
.around(LBRACK).none() // [
.before(RBRACK).none() // ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.intellij.application.options.IndentOptionsEditor
import com.intellij.application.options.SmartIndentOptionsEditor
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.SPACES_OTHER
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.WRAPPING_FIELDS_VARIABLES_GROUPS
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
import com.tang.intellij.lua.lang.LuaLanguage

Expand All @@ -40,6 +40,12 @@ class LuaLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider()
return SmartIndentOptionsEditor()
}

override fun getDefaultCommonSettings(): CommonCodeStyleSettings {
val commonSettings = CommonCodeStyleSettings(language)
commonSettings.initIndentOptions()
return commonSettings
}

override fun customizeSettings(consumer: CodeStyleSettingsCustomizable, settingsType: LanguageCodeStyleSettingsProvider.SettingsType) {
when (settingsType) {
LanguageCodeStyleSettingsProvider.SettingsType.SPACING_SETTINGS -> {
Expand Down

0 comments on commit acd497f

Please sign in to comment.