Skip to content

Commit d77d069

Browse files
committed
code style settings
1 parent 9d20b39 commit d77d069

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

src/main/java/com/tang/intellij/lua/editor/formatter/LuaCodeStyleSettings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
* LuaCodeStyleSettings
2525
* Created by tangzx on 2017/2/22.
2626
*/
27-
class LuaCodeStyleSettings extends CustomCodeStyleSettings {
27+
public class LuaCodeStyleSettings extends CustomCodeStyleSettings {
28+
29+
public boolean SPACE_AFTER_TABLE_FIELD_SEP = true;
2830

2931
LuaCodeStyleSettings(CodeStyleSettings container) {
3032
super(LuaLanguage.INSTANCE.getID(), container);

src/main/java/com/tang/intellij/lua/editor/formatter/LuaCodeStyleSettingsProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ protected CodeStyleAbstractPanel createPanel(CodeStyleSettings codeStyleSettings
5151
return new TabbedLanguageCodeStylePanel(language, currentSettings, settings) {
5252
@Override
5353
protected void initTabs(CodeStyleSettings settings) {
54-
addIndentOptionsTab(settings);
54+
//addIndentOptionsTab(settings);
5555
addSpacesTab(settings);
56-
addBlankLinesTab(settings);
57-
addWrappingAndBracesTab(settings);
56+
//addBlankLinesTab(settings);
57+
//addWrappingAndBracesTab(settings);
5858
}
5959
};
6060
}

src/main/java/com/tang/intellij/lua/editor/formatter/LuaFormattingModelBuilder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,27 @@ public FormattingModel createModel(PsiElement element, CodeStyleSettings setting
4848
}
4949

5050
private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) {
51+
final LuaCodeStyleSettings luaCodeStyleSettings = settings.getCustomSettings(LuaCodeStyleSettings.class);
52+
5153
return new SpacingBuilder(settings, LuaLanguage.INSTANCE)
5254
.before(END).lineBreakInCode()
5355
.after(DO).lineBreakInCode()
5456
.after(THEN).lineBreakInCode()
5557
.after(LOCAL).spaces(1) //local<SPACE>
56-
.after(COMMA).spaces(1) //,<SPACE>
58+
.after(COMMA).spaces(settings.SPACE_AFTER_COMMA ? 1 : 0) //,<SPACE>
5759
.between(LCURLY, TABLE_FIELD).spaces(1) // {<SPACE>1, 2 }
5860
.between(TABLE_FIELD, RCURLY).spaces(1) // { 1, 2<SPACE>}
59-
.after(TABLE_FIELD_SEP).spaces(1) // { 1,<SPACE>2 }
61+
.after(TABLE_FIELD_SEP).spaces(luaCodeStyleSettings.SPACE_AFTER_TABLE_FIELD_SEP ? 1 : 0) // { 1,<SPACE>2 }
6062
.before(BLOCK).blankLines(0)
6163
.afterInside(RPAREN, FUNC_BODY).lineBreakInCode()
6264
.between(FUNCTION, FUNC_BODY).none()
6365
.between(FUNCTION, NAME_DEF).spaces(1) //function<SPACE>name()
6466
.between(VALUE_EXPR, COMMA).lineBreakOrForceSpace(false, false)
65-
.around(BINARY_OP).spaces(1)
67+
.around(BINARY_OP).spaces(settings.SPACE_AROUND_ASSIGNMENT_OPERATORS ? 1 : 0)
6668
.around(UNARY_OP).spaces(1)
67-
.around(ASSIGN).lineBreakOrForceSpace(false, true) // = 号两头不能换行
69+
.around(ASSIGN).lineBreakOrForceSpace(false, settings.SPACE_AROUND_ASSIGNMENT_OPERATORS) // = 号两头不能换行
6870
.around(LuaSyntaxHighlighter.KEYWORD_TOKENS).spaces(1)
69-
.before(COMMA).spaces(0)
71+
.before(COMMA).spaces(settings.SPACE_BEFORE_COMMA ? 1 : 0)
7072
.before(SEMI).spaces(0);
7173
}
7274

src/main/java/com/tang/intellij/lua/editor/formatter/LuaLanguageCodeStyleSettingsProvider.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package com.tang.intellij.lua.editor.formatter;
1818

19+
import com.intellij.application.options.CodeStyleAbstractPanel;
1920
import com.intellij.lang.Language;
21+
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable;
2022
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
2123
import com.tang.intellij.lua.lang.LuaLanguage;
2224
import org.jetbrains.annotations.NotNull;
2325

26+
import static com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.SPACES_OTHER;
27+
2428
/**
2529
*
2630
* Created by tangzx on 2017/2/22.
@@ -34,6 +38,16 @@ public Language getLanguage() {
3438

3539
@Override
3640
public String getCodeSample(@NotNull SettingsType settingsType) {
37-
return "local a";
41+
return CodeStyleAbstractPanel.readFromFile(this.getClass(), "preview.lua.template");
42+
}
43+
44+
@Override
45+
public void customizeSettings(@NotNull CodeStyleSettingsCustomizable consumer, @NotNull SettingsType settingsType) {
46+
if (settingsType == SettingsType.SPACING_SETTINGS) {
47+
consumer.showCustomOption(LuaCodeStyleSettings.class, "SPACE_AFTER_TABLE_FIELD_SEP", "After field sep", SPACES_OTHER);
48+
consumer.showStandardOptions("SPACE_AROUND_ASSIGNMENT_OPERATORS",
49+
"SPACE_BEFORE_COMMA",
50+
"SPACE_AFTER_COMMA");
51+
}
3852
}
3953
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local var = 1 -- a short comment
2+
--- doc comment
3+
--- @param par1 Par1Type @some strings
4+
function var:fun(par1, par2)
5+
print('hello')
6+
return self.len + 2
7+
end
8+
9+
function globalFun()
10+
return "string" .. var
11+
end
12+
13+
globalVar = {
14+
property = value
15+
}

0 commit comments

Comments
 (0)