diff --git a/docs/Settings.md b/docs/Settings.md index 7cd9f6e3..23472e75 100644 --- a/docs/Settings.md +++ b/docs/Settings.md @@ -194,6 +194,12 @@ Issue code when an ordinal value exceeds its limits. (default severity: error) +#### `indentation` + +Controls the indentation string when formatting or serializing yang files. + + (default: four spaces) + #### `invalid-config` Issue code when a `config=true` is a child of a `config=false` (see https://tools.ietf.org/html/rfc7950#section-7.21.1) diff --git a/yang-lsp/io.typefox.yang/src/main/java/io/typefox/yang/formatting2/YangFormatter.xtend b/yang-lsp/io.typefox.yang/src/main/java/io/typefox/yang/formatting2/YangFormatter.xtend index 0efee80d..65a983f5 100644 --- a/yang-lsp/io.typefox.yang/src/main/java/io/typefox/yang/formatting2/YangFormatter.xtend +++ b/yang-lsp/io.typefox.yang/src/main/java/io/typefox/yang/formatting2/YangFormatter.xtend @@ -100,12 +100,13 @@ import org.eclipse.xtext.preferences.MapBasedPreferenceValues import org.eclipse.xtext.xbase.lib.Procedures.Procedure1 import static io.typefox.yang.yang.YangPackage.Literals.* +import org.eclipse.xtext.preferences.IPreferenceValuesProvider class YangFormatter extends AbstractFormatter2 { @Inject extension YangGrammarAccess @Inject IIndentationInformation indentationInformation - + @Inject IPreferenceValuesProvider yangSettings // Option Keys public static val FORCE_NEW_LINE = new BooleanKey("FORCE_NEW_LINE", true) @@ -116,9 +117,14 @@ class YangFormatter extends AbstractFormatter2 { val preferences = request.preferences if (preferences instanceof MapBasedPreferenceValues) { val configuredIndent = preferences.getPreference(FormatterPreferenceKeys.indentation) + // Overwrite if not already present. Might be configured with FormattingOptions from IDE if(configuredIndent === null || preferences.values.get(FormatterPreferenceKeys.indentation.id) === null) { - // Overwrite if not already present. Might be configured with FormattingOptions from IDE - preferences.put(FormatterPreferenceKeys.indentation, indentationInformation.indentString) + var defaultIndent = yangSettings.getPreferenceValues(request.textRegionAccess.resource).getPreference(FormatterPreferenceKeys.indentation) + // if not set using yang.setting. Ask IIndentationInformation + if(defaultIndent == FormatterPreferenceKeys.indentation.defaultValue) { + defaultIndent = indentationInformation.indentString + } + preferences.put(FormatterPreferenceKeys.indentation, defaultIndent) } } super.initialize(request)