Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11657 for trunk/sources


Ignore:
Timestamp:
12/04/14 17:14:01 (10 years ago)
Author:
jkarder
Message:

#2262: applied some of the changes suggested by swagner in comment:17:ticket:2262

  • added highlighting of current line
  • added error markers and bookmarks
  • fixed <Ctrl> + <Backspace> bug
  • minor code changes
Location:
trunk/sources
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CodeEditor/3.3/CodeEditor.Designer.cs

    r7967 r11657  
    4646      // textEditor
    4747      //
    48       this.textEditor.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    49                   | System.Windows.Forms.AnchorStyles.Left)
    50                   | System.Windows.Forms.AnchorStyles.Right)));
     48      this.textEditor.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     49            | System.Windows.Forms.AnchorStyles.Left)
     50            | System.Windows.Forms.AnchorStyles.Right)));
    5151      this.textEditor.ConvertTabsToSpaces = true;
    5252      this.textEditor.IndentStyle = ICSharpCode.TextEditor.Document.IndentStyle.Auto;
    5353      this.textEditor.IsIconBarVisible = true;
    5454      this.textEditor.IsReadOnly = false;
     55      this.textEditor.LineViewerStyle = ICSharpCode.TextEditor.Document.LineViewerStyle.FullRow;
    5556      this.textEditor.Location = new System.Drawing.Point(0, 0);
    5657      this.textEditor.Name = "textEditor";
     
    9798      this.sharpDevelopLabel.Text = "powered by #develop";
    9899      this.sharpDevelopLabel.ToolTipText = "Syntax highlighting and code completion facilities provided through #develop libr" +
    99           "aries";
     100    "aries";
    100101      this.sharpDevelopLabel.Click += new System.EventHandler(this.toolStripStatusLabel1_Click);
    101102      //
     
    108109      // CodeEditor
    109110      //
    110       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    111111      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    112112      this.Controls.Add(this.textEditor);
  • trunk/sources/HeuristicLab.CodeEditor/3.3/CodeEditor.cs

    r11436 r11657  
    4646
    4747    #region Fields & Properties
     48    private static Color WarningColor = Color.Blue;
     49    private static Color ErrorColor = Color.Red;
    4850
    4951    internal Dom.ProjectContentRegistry projectContentRegistry;
     
    127129      }
    128130      set {
     131        if (Doc.TextContent == value) return;
    129132        Doc.Replace(prefix.Length, Doc.TextLength - suffix.Length - prefix.Length, value);
    130133        Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
     
    141144
    142145    public event EventHandler TextEditorValidated;
    143 
    144146    protected void OnTextEditorValidated() {
    145147      if (TextEditorValidated != null)
     
    148150
    149151    public event EventHandler TextEditorTextChanged;
    150 
    151152    protected void OnTextEditorTextChanged() {
    152153      if (TextEditorTextChanged != null)
     
    159160      textEditor.ActiveTextAreaControl.TextEditorProperties.SupportReadOnlySegments = true;
    160161
    161       textEditor.SetHighlighting("C#");
    162       textEditor.ShowEOLMarkers = false;
    163       textEditor.ShowInvalidLines = false;
     162      LoadHighlightingStrategy();
    164163      HostCallbackImplementation.Register(this);
    165164      CodeCompletionKeyHandler.Attach(this, textEditor);
     
    192191        return;
    193192
    194       textEditor.ActiveTextAreaControl.TextArea.KeyEventHandler += new ICSharpCode.TextEditor.KeyEventHandler(TextArea_KeyEventHandler);
    195       textEditor.ActiveTextAreaControl.TextArea.DoProcessDialogKey += new DialogKeyProcessor(TextArea_DoProcessDialogKey);
    196 
    197       parserThread = new Thread(ParserThread);
    198       parserThread.IsBackground = true;
     193      textEditor.ActiveTextAreaControl.TextArea.KeyEventHandler += TextArea_KeyEventHandler;
     194      textEditor.ActiveTextAreaControl.TextArea.DoProcessDialogKey += TextArea_DoProcessDialogKey;
     195
     196      parserThread = new Thread(ParserThread) { IsBackground = true };
    199197      parserThread.Start();
    200198
    201       textEditor.Validated += (s, a) => { OnTextEditorValidated(); };
    202       textEditor.TextChanged += (s, a) => { OnTextEditorTextChanged(); };
     199      textEditor.Validated += (s, a) => OnTextEditorValidated();
     200      textEditor.TextChanged += (s, a) => {
     201        Doc.MarkerStrategy.RemoveAll(m => errorMarkers.Contains(m)); errorMarkers.Clear();
     202        Doc.BookmarkManager.RemoveMarks(m => errorBookmarks.Contains(m)); errorBookmarks.Clear();
     203        Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
     204        Doc.CommitUpdate();
     205        OnTextEditorTextChanged();
     206      };
    203207      InitializeImageList();
     208    }
     209
     210    private void LoadHighlightingStrategy() {
     211      var strategy = (DefaultHighlightingStrategy)HighlightingManager.Manager.FindHighlighter("C#");
     212      strategy.SetColorFor("CaretMarker", new HighlightColor(Color.Beige, false, false));
     213      Doc.HighlightingStrategy = strategy;
    204214    }
    205215
     
    216226
    217227    #region keyboard handlers: filter input in read-only areas
    218 
    219228    bool TextArea_KeyEventHandler(char ch) {
    220229      int caret = textEditor.ActiveTextAreaControl.Caret.Offset;
     
    232241      return false;
    233242    }
    234 
    235243    #endregion
    236244
     
    240248    }
    241249
     250    public void ScrollToPosition(int line, int column) {
     251      var segment = GetSegmentAtOffset(line, column);
     252      var position = Doc.OffsetToPosition(segment.Offset + segment.Length);
     253      var caret = textEditor.ActiveTextAreaControl.Caret;
     254      caret.Position = position;
     255      textEditor.ActiveTextAreaControl.CenterViewOn(line, 0);
     256    }
     257
    242258    private List<TextMarker> errorMarkers = new List<TextMarker>();
    243259    private List<Bookmark> errorBookmarks = new List<Bookmark>();
    244260    public void ShowCompileErrors(CompilerErrorCollection compilerErrors, string filename) {
    245       Doc.MarkerStrategy.RemoveAll(m => errorMarkers.Contains(m));
    246       Doc.BookmarkManager.RemoveMarks(m => errorBookmarks.Contains(m));
    247       errorMarkers.Clear();
    248       errorBookmarks.Clear();
    249261      errorLabel.Text = "";
    250262      errorLabel.ToolTipText = null;
     
    273285    private void AddErrorMarker(CompilerError error) {
    274286      var segment = GetSegmentAtOffset(error.Line, error.Column);
    275       Color color = error.IsWarning ? Color.Blue : Color.Red;
     287      Color color = error.IsWarning ? WarningColor : ErrorColor;
    276288      var marker = new TextMarker(segment.Offset, segment.Length, TextMarkerType.WaveLine, color) {
    277289        ToolTip = error.ErrorText,
     
    282294
    283295    private void AddErrorBookmark(CompilerError error) {
    284       var bookmark = new ErrorBookmark(Doc, new TextLocation(error.Column, error.Line - 1));
     296      Color color = error.IsWarning ? WarningColor : ErrorColor;
     297      var bookmark = new ErrorBookmark(Doc, new TextLocation(error.Column, error.Line - 1), color);
    285298      errorBookmarks.Add(bookmark);
    286299      Doc.BookmarkManager.AddMark(bookmark);
     
    288301
    289302    private AbstractSegment GetSegmentAtOffset(int lineNr, int columnNr) {
    290       lineNr = Math.Max(Doc.OffsetToPosition(prefix.Length).Line, lineNr);
     303      lineNr = Math.Max(Doc.OffsetToPosition(prefix.Length).Line, lineNr - 1);
    291304      lineNr = Math.Min(Doc.OffsetToPosition(Doc.TextLength - suffix.Length).Line, lineNr);
    292       var line = Doc.GetLineSegment(lineNr - 1);
    293       columnNr = Math.Max(0, columnNr);
     305      var line = Doc.GetLineSegment(lineNr);
     306      columnNr = Math.Max(0, columnNr - 1);
    294307      columnNr = Math.Min(line.Length, columnNr);
    295308      var word = line.GetWord(columnNr);
     
    299312        segment.Length = word.Length;
    300313      } else {
    301         segment.Offset = line.Offset + columnNr - 1;
     314        segment.Offset = line.Offset + columnNr;
    302315        segment.Length = 1;
    303316      }
  • trunk/sources/HeuristicLab.CodeEditor/3.3/ErrorBookmark.cs

    r11171 r11657  
    2525namespace HeuristicLab.CodeEditor {
    2626  public class ErrorBookmark : Bookmark {
     27    private readonly Brush brush;
    2728
    2829    public override bool CanToggle { get { return false; } }
    2930
    3031    public ErrorBookmark(IDocument document, TextLocation location)
     32      : this(document, location, Color.Red) { }
     33
     34    public ErrorBookmark(IDocument document, TextLocation location, Color color)
    3135      : base(document, location) {
     36      brush = new SolidBrush(color);
    3237    }
    3338
    34     public override void Draw(IconBarMargin margin, System.Drawing.Graphics g, System.Drawing.Point p) {
     39    public override void Draw(IconBarMargin margin, Graphics g, Point p) {
    3540      int delta = margin.TextArea.TextView.FontHeight / 4;
    3641      Rectangle rect = new Rectangle(
     
    3944        margin.DrawingPosition.Width - 6,
    4045        margin.TextArea.TextView.FontHeight - delta * 2);
    41       g.FillRectangle(Brushes.Red, rect);
     46      g.FillRectangle(brush, rect);
    4247      g.DrawRectangle(Pens.White, rect);
    4348    }
  • trunk/sources/HeuristicLab.CodeEditor/3.3/HeuristicLab.CodeEditor-3.3.csproj

    r11623 r11657  
    151151  <ItemGroup>
    152152    <Compile Include="CodeCompletionData.cs" />
    153     <EmbeddedResource Include="CodeEditor.resx">
    154       <DependentUpon>CodeEditor.cs</DependentUpon>
    155     </EmbeddedResource>
    156153    <EmbeddedResource Include="CodeViewer.resx">
    157154      <DependentUpon>CodeViewer.cs</DependentUpon>
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs

    r11171 r11657  
    132132          return true;
    133133        case Keys.F6:
    134           if (!Running) Compile();
     134          if (!Running) base.ProcessCmdKey(ref msg, keyData);
    135135          return true;
    136136      }
    137       return base.ProcessCmdKey(ref msg, keyData); ;
     137      return base.ProcessCmdKey(ref msg, keyData);
    138138    }
    139139    #endregion
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/ScriptView.Designer.cs

    r10747 r11657  
    110110      this.toolTip.SetToolTip(this.compileButton, "Compile (F6)");
    111111      this.compileButton.UseVisualStyleBackColor = true;
    112       this.compileButton.Click += new System.EventHandler(this.CompileButtonOnClick);
     112      this.compileButton.Click += new System.EventHandler(this.compileButton_Click);
    113113      //
    114114      // infoTabControl
     
    178178      this.errorListView.UseCompatibleStateImageBehavior = false;
    179179      this.errorListView.View = System.Windows.Forms.View.Details;
     180      this.errorListView.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.errorListView_MouseDoubleClick);
    180181      //
    181182      // iconColumnHeader
     
    213214      this.codeEditor.TabIndex = 0;
    214215      this.codeEditor.UserCode = "";
    215       this.codeEditor.TextEditorTextChanged += new System.EventHandler(this.CodeEditorOnTextEditorTextChanged);
     216      this.codeEditor.TextEditorTextChanged += new System.EventHandler(this.codeEditor_TextEditorTextChanged);
    216217      //
    217218      // splitContainer1
    218219      //
    219       this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    220             | System.Windows.Forms.AnchorStyles.Left)
     220      this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     221            | System.Windows.Forms.AnchorStyles.Left) 
    221222            | System.Windows.Forms.AnchorStyles.Right)));
    222223      this.splitContainer1.Location = new System.Drawing.Point(0, 56);
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/ScriptView.cs

    r11480 r11657  
    3535  [Content(typeof(Script), true)]
    3636  public partial class ScriptView : NamedItemView {
     37    #region Properties
    3738    public new Script Content {
    3839      get { return (Script)base.Content; }
    3940      set { base.Content = value; }
    40     }
    41 
    42     public ScriptView() {
    43       InitializeComponent();
    44       errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
    4541    }
    4642
     
    5450      set { base.Locked = codeEditor.ReadOnly = value; }
    5551    }
     52    #endregion
     53
     54    public ScriptView() {
     55      InitializeComponent();
     56      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
     57    }
    5658
    5759    protected override void RegisterContentEvents() {
    5860      base.RegisterContentEvents();
    59       Content.CodeChanged += ContentOnCodeChanged;
     61      Content.CodeChanged += Content_CodeChanged;
    6062    }
    6163
    6264    protected override void DeregisterContentEvents() {
    63       Content.CodeChanged -= ContentOnCodeChanged;
     65      Content.CodeChanged -= Content_CodeChanged;
    6466      base.DeregisterContentEvents();
    6567    }
    6668
    67     protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
    68       codeEditor.UserCode = Content.Code;
    69     }
    70 
     69    #region Overrides
    7170    protected override void OnContentChanged() {
    7271      base.OnContentChanged();
     
    7473        codeEditor.UserCode = string.Empty;
    7574      } else {
    76         codeEditor.UserCode = Content.Code;
     75        if (codeEditor.UserCode != Content.Code)
     76          codeEditor.UserCode = Content.Code;
    7777        foreach (var asm in Content.GetAssemblies())
    7878          codeEditor.AddAssembly(asm);
     
    9696    }
    9797
    98     protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
    99       Compile();
    100     }
    101 
    102     protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
    103       if (Content == null) return;
    104       Content.Code = codeEditor.UserCode;
    105     }
    10698    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    10799      switch (keyData) {
     
    113105      return base.ProcessCmdKey(ref msg, keyData);
    114106    }
     107    #endregion
    115108
    116109    public virtual bool Compile() {
     
    123116        outputTextBox.AppendText("Compilation succeeded.");
    124117        return true;
    125       } catch {
     118      } catch (InvalidOperationException) {
    126119        if (Content.CompileErrors.HasErrors) {
    127120          outputTextBox.AppendText("Compilation failed.");
     
    137130        ReadOnly = false;
    138131        Locked = false;
     132        codeEditor.Focus();
    139133        OnContentChanged();
    140134      }
    141135    }
    142136
     137    #region Helpers
    143138    protected virtual void ShowCompilationResults() {
    144139      if (Content.CompileErrors.Count == 0) return;
    145       var msgs = Content.CompileErrors.OfType<CompilerError>()
     140
     141      var messages = Content.CompileErrors.OfType<CompilerError>()
    146142                                      .OrderBy(x => x.IsWarning)
    147143                                      .ThenBy(x => x.Line)
    148144                                      .ThenBy(x => x.Column);
    149       foreach (var m in msgs) {
    150         var item = new ListViewItem();
     145
     146      foreach (var m in messages) {
     147        var item = new ListViewItem { Tag = m };
    151148        item.SubItems.AddRange(new[] {
    152149          m.IsWarning ? "Warning" : "Error",
     
    159156        errorListView.Items.Add(item);
    160157      }
     158
     159      codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs");
     160
    161161      AdjustErrorListViewColumnSizes();
    162162    }
     
    167167        ch.Width = -2;
    168168    }
     169    #endregion
     170
     171    #region Event Handlers
     172    private void Content_CodeChanged(object sender, EventArgs e) {
     173      if (InvokeRequired)
     174        Invoke(new EventHandler(Content_CodeChanged), sender, e);
     175      else {
     176        codeEditor.UserCode = Content.Code;
     177      }
     178    }
     179
     180    private void compileButton_Click(object sender, EventArgs e) {
     181      Compile();
     182    }
     183
     184    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
     185      if (Content == null) return;
     186      Content.Code = codeEditor.UserCode;
     187    }
     188
     189    private void errorListView_MouseDoubleClick(object sender, MouseEventArgs e) {
     190      if (e.Button == MouseButtons.Left) {
     191        var item = errorListView.SelectedItems[0];
     192        var message = (CompilerError)item.Tag;
     193        codeEditor.ScrollToPosition(message.Line, message.Column);
     194        codeEditor.Focus();
     195      }
     196    }
     197    #endregion
    169198  }
    170199}
  • trunk/sources/HeuristicLab.Scripting/3.3/Script.cs

    r11477 r11657  
    2424using System.CodeDom.Compiler;
    2525using System.Collections.Generic;
    26 using System.Diagnostics;
    2726using System.Drawing;
    2827using System.IO;
     
    8079    public Script()
    8180      : base("Script", "An empty script.") {
    82       code = string.Empty;
     81      code = CodeTemplate;
    8382    }
    8483    public Script(string code)
     
    109108        WarningLevel = 4
    110109      };
     110
    111111      parameters.ReferencedAssemblies.AddRange(
    112112        GetAssemblies()
    113113        .Select(a => a.Location)
    114114        .ToArray());
    115       var unit = CreateCompilationUnit();
    116       var writer = new StringWriter();
    117       CodeProvider.GenerateCodeFromCompileUnit(
    118         unit,
    119         writer,
    120         new CodeGeneratorOptions {
    121           ElseOnClosing = true,
    122           IndentString = "  ",
    123         });
    124       return CodeProvider.CompileAssemblyFromDom(parameters, unit);
     115
     116      return CodeProvider.CompileAssemblyFromSource(parameters, code);
    125117    }
    126118
     
    135127            .AppendLine(error.ErrorText);
    136128        }
    137         throw new Exception(string.Format("Compilation of \"{0}\" failed:{1}{2}",
     129        throw new InvalidOperationException(string.Format("Compilation of \"{0}\" failed:{1}{2}",
    138130          Name, Environment.NewLine, sb.ToString()));
    139131      } else {
     
    143135
    144136    public virtual IEnumerable<Assembly> GetAssemblies() {
    145       var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && File.Exists(a.Location)).ToList();   
     137      var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && File.Exists(a.Location)).ToList();
    146138      assemblies.Add(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly); // for dlr functionality
    147139      return assemblies;
Note: See TracChangeset for help on using the changeset viewer.