Skip to content

Commit

Permalink
Use IDE provided formatting options #229
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Nov 3, 2023
1 parent db61712 commit c93cf4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class YangContentProposalProvider extends IdeContentProposalProvider {
label = "import " + e.qualifiedName
proposal = '''
import «e.qualifiedName» {
prefix «m.substatements.filter(Prefix).head?.prefix»;
«IF rev!==null»
revision-date «rev.revision»;
«ENDIF»
prefix «m.substatements.filter(Prefix).head?.prefix»;
«IF rev!==null»
revision-date «rev.revision»;
«ENDIF»
}
'''.toString.replaceAll(' ', indentString);
description = "module " + m.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.eclipse.xtext.preferences.MapBasedPreferenceValues
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.util.TextRegion
import com.google.common.base.Strings
import org.eclipse.xtext.formatting2.FormatterPreferenceKeys

class YangFormattingService extends FormattingService {

Expand All @@ -24,7 +25,7 @@ class YangFormattingService extends FormattingService {
}
}
val preferences = newHashMap
preferences.put("indentation", indent)
preferences.put(FormatterPreferenceKeys.indentation.id, indent)
val replacements = format2(resource, new TextRegion(offset, length), new MapBasedPreferenceValues(preferences))
return replacements.map [ r |
document.toTextEdit(r.replacementText, r.offset, r.length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class YangFormatter extends AbstractFormatter2 {
override protected initialize(FormatterRequest request) {
val preferences = request.preferences
if (preferences instanceof MapBasedPreferenceValues) {
preferences.put(FormatterPreferenceKeys.indentation, indentationInformation.indentString)
val configuredIndent = preferences.getPreference(FormatterPreferenceKeys.indentation)
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)
}
}
super.initialize(request)
}
Expand Down

0 comments on commit c93cf4f

Please sign in to comment.