Skip to content

Commit

Permalink
Use yang.settings indentation if set #226
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Nov 3, 2023
1 parent c93cf4f commit 2ce0935
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2ce0935

Please sign in to comment.