Browse Source

Fix #506: Tabs/spaces not shown in text editor for C# files

pull/512/head
Andreas Weizel 11 years ago
parent
commit
dd292ff7f0
  1. 224
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpLanguageBinding.cs

224
src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpLanguageBinding.cs

@ -20,6 +20,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Reflection;
using System.Threading; using System.Threading;
using ICSharpCode.AvalonEdit; using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
@ -83,9 +84,9 @@ namespace CSharpBinding
// Create instance of options adapter and register it as service // Create instance of options adapter and register it as service
var formattingPolicy = CSharpFormattingPolicies.Instance.GetProjectOptions( var formattingPolicy = CSharpFormattingPolicies.Instance.GetProjectOptions(
SD.ProjectService.FindProjectContainingFile(editor.FileName)); SD.ProjectService.FindProjectContainingFile(editor.FileName));
options = new CodeEditorFormattingOptionsAdapter(editor.Options, formattingPolicy.OptionsContainer);
var textEditor = editor.GetService<TextEditor>(); var textEditor = editor.GetService<TextEditor>();
if (textEditor != null) { if (textEditor != null) {
options = new CodeEditorFormattingOptionsAdapter(textEditor.Options, editor.Options, formattingPolicy.OptionsContainer);
var textViewServices = textEditor.TextArea.TextView.Services; var textViewServices = textEditor.TextArea.TextView.Services;
// Unregister any previous ITextEditorOptions instance from editor, if existing, register our impl. // Unregister any previous ITextEditorOptions instance from editor, if existing, register our impl.
@ -94,7 +95,7 @@ namespace CSharpBinding
// Set TextEditor's options to same object // Set TextEditor's options to same object
originalEditorOptions = textEditor.Options; originalEditorOptions = textEditor.Options;
textEditor.Options = options; textEditor.Options = options.TextEditorOptions;
} }
} }
@ -110,7 +111,7 @@ namespace CSharpBinding
textView.Services.RemoveService(typeof(ITextEditorOptions)); textView.Services.RemoveService(typeof(ITextEditorOptions));
// Reset TextEditor options, too? // Reset TextEditor options, too?
if ((textEditor.Options != null) && (textEditor.Options == options)) if ((textEditor.Options != null) && (textEditor.Options == options.TextEditorOptions))
textEditor.Options = originalEditorOptions; textEditor.Options = originalEditorOptions;
} }
@ -128,225 +129,144 @@ namespace CSharpBinding
} }
} }
class CodeEditorFormattingOptionsAdapter : TextEditorOptions, ITextEditorOptions, ICodeEditorOptions class CodeEditorFormattingOptionsAdapter : ITextEditorOptions, INotifyPropertyChanged
{ {
CSharpFormattingOptionsContainer container; CSharpFormattingOptionsContainer container;
readonly ITextEditorOptions globalOptions; readonly TextEditorOptions avalonEditOptions;
readonly ICodeEditorOptions globalCodeEditorOptions; readonly TextEditorOptions originalAvalonEditOptions;
readonly ITextEditorOptions originalSDOptions;
public CodeEditorFormattingOptionsAdapter(ITextEditorOptions globalOptions, CSharpFormattingOptionsContainer container) public CodeEditorFormattingOptionsAdapter(TextEditorOptions originalAvalonEditOptions, ITextEditorOptions originalSDOptions, CSharpFormattingOptionsContainer container)
{ {
if (globalOptions == null) if (originalAvalonEditOptions == null)
throw new ArgumentNullException("globalOptions"); throw new ArgumentNullException("originalAvalonEditOptions");
if (originalSDOptions == null)
throw new ArgumentNullException("originalSDOptions");
if (container == null) if (container == null)
throw new ArgumentNullException("container"); throw new ArgumentNullException("container");
this.globalOptions = globalOptions; this.originalAvalonEditOptions = originalAvalonEditOptions;
this.globalCodeEditorOptions = globalOptions as ICodeEditorOptions; this.avalonEditOptions = new TextEditorOptions(originalAvalonEditOptions);
this.originalSDOptions = originalSDOptions;
this.container = container; this.container = container;
// Update overridden options once
UpdateOverriddenProperties();
CSharpFormattingPolicies.Instance.FormattingPolicyUpdated += OnFormattingPolicyUpdated; CSharpFormattingPolicies.Instance.FormattingPolicyUpdated += OnFormattingPolicyUpdated;
globalOptions.PropertyChanged += OnGlobalOptionsPropertyChanged; this.originalAvalonEditOptions.PropertyChanged += OnOrigAvalonOptionsPropertyChanged;
this.originalSDOptions.PropertyChanged += OnSDOptionsPropertyChanged;
} }
void OnFormattingPolicyUpdated(object sender, CSharpBinding.FormattingStrategy.CSharpFormattingPolicyUpdateEventArgs e) void OnFormattingPolicyUpdated(object sender, CSharpBinding.FormattingStrategy.CSharpFormattingPolicyUpdateEventArgs e)
{ {
// Update editor options from changed policy
UpdateOverriddenProperties();
OnPropertyChanged("IndentationSize"); OnPropertyChanged("IndentationSize");
OnPropertyChanged("IndentationString");
OnPropertyChanged("ConvertTabsToSpaces"); OnPropertyChanged("ConvertTabsToSpaces");
} }
void OnGlobalOptionsPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) void UpdateOverriddenProperties()
{ {
OnPropertyChanged(e.PropertyName); avalonEditOptions.IndentationSize = container.GetEffectiveIndentationSize() ?? originalSDOptions.IndentationSize;
} avalonEditOptions.ConvertTabsToSpaces = container.GetEffectiveConvertTabsToSpaces() ?? originalSDOptions.ConvertTabsToSpaces;
#region ITextEditorOptions implementation
public override int IndentationSize {
get {
return container.GetEffectiveIndentationSize() ?? globalOptions.IndentationSize;
}
} }
public override bool ConvertTabsToSpaces { void OnOrigAvalonOptionsPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
get { {
return container.GetEffectiveConvertTabsToSpaces() ?? globalOptions.ConvertTabsToSpaces; if ((e.PropertyName != "IndentationSize") && (e.PropertyName != "IndentationString") && (e.PropertyName != "ConvertTabsToSpaces")) {
} // Update values in our own TextEditorOptions instance
PropertyInfo propertyInfo = typeof(TextEditorOptions).GetProperty(e.PropertyName);
if (propertyInfo != null) {
propertyInfo.SetValue(avalonEditOptions, propertyInfo.GetValue(originalAvalonEditOptions));
} }
} else {
public bool AutoInsertBlockEnd { UpdateOverriddenProperties();
get {
return globalOptions.AutoInsertBlockEnd;
} }
OnPropertyChanged(e.PropertyName);
} }
public int VerticalRulerColumn { void OnSDOptionsPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
get { {
return globalOptions.VerticalRulerColumn; OnPropertyChanged(e.PropertyName);
}
} }
public bool UnderlineErrors { public event PropertyChangedEventHandler PropertyChanged;
get {
return globalOptions.UnderlineErrors;
}
}
public string FontFamily { void OnPropertyChanged(string propertyName)
get { {
return globalOptions.FontFamily; if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }
public double FontSize { public TextEditorOptions TextEditorOptions
{
get { get {
return globalOptions.FontSize; return avalonEditOptions;
} }
} }
#endregion #region Overridden properties
public override bool AllowScrollBelowDocument { public int IndentationSize {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.AllowScrollBelowDocument : default(bool); // Get value from own TextEditorOptions instance
} return avalonEditOptions.IndentationSize;
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.AllowScrollBelowDocument = value;
}
} }
} }
public bool ShowLineNumbers { public string IndentationString {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.ShowLineNumbers : default(bool); // Get value from own TextEditorOptions instance
} return avalonEditOptions.IndentationString;
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.ShowLineNumbers = value;
}
} }
} }
public bool EnableChangeMarkerMargin { public bool ConvertTabsToSpaces {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.EnableChangeMarkerMargin : default(bool); // Get value from own TextEditorOptions instance
} return avalonEditOptions.ConvertTabsToSpaces;
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.EnableChangeMarkerMargin = value;
}
} }
} }
public bool WordWrap { #endregion
get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.WordWrap : default(bool);
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.WordWrap = value;
}
}
}
public bool CtrlClickGoToDefinition { #region Rest of ITextEditorOptions implementation
get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.CtrlClickGoToDefinition : default(bool);
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.CtrlClickGoToDefinition = value;
}
}
}
public bool MouseWheelZoom { public bool AutoInsertBlockEnd {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.MouseWheelZoom : default(bool); return originalSDOptions.AutoInsertBlockEnd;
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.MouseWheelZoom = value;
}
} }
} }
public bool HighlightBrackets { public int VerticalRulerColumn {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.HighlightBrackets : default(bool); return originalSDOptions.VerticalRulerColumn;
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.HighlightBrackets = value;
}
} }
} }
public bool HighlightSymbol { public bool UnderlineErrors {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.HighlightSymbol : default(bool); return originalSDOptions.UnderlineErrors;
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.HighlightSymbol = value;
}
} }
} }
public bool EnableAnimations { public string FontFamily {
get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.EnableAnimations : default(bool);
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.EnableAnimations = value;
}
}
}
public bool UseSmartIndentation {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.UseSmartIndentation : default(bool); return originalSDOptions.FontFamily;
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.UseSmartIndentation = value;
}
} }
} }
public bool EnableFolding { public double FontSize {
get { get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.EnableFolding : default(bool); return originalSDOptions.FontSize;
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.EnableFolding = value;
}
} }
} }
public bool EnableQuickClassBrowser { #endregion
get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.EnableQuickClassBrowser : default(bool);
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.EnableQuickClassBrowser = value;
}
}
}
public bool ShowHiddenDefinitions {
get {
return (globalCodeEditorOptions != null) ? globalCodeEditorOptions.ShowHiddenDefinitions : default(bool);
}
set {
if (globalCodeEditorOptions != null) {
globalCodeEditorOptions.ShowHiddenDefinitions = value;
}
}
}
} }
} }

Loading…
Cancel
Save