Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.2/HeuristicLab.CodeEditor/3.3/CodeEditor.cs @ 4894

Last change on this file since 4894 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 15.1 KB
Line 
1// CSharp Editor Example with Code Completion
2// Copyright (c) 2006, Daniel Grunwald
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// - Redistributions of source code must retain the above copyright notice, this list
9//   of conditions and the following disclaimer.
10//
11// - Redistributions in binary form must reproduce the above copyright notice, this list
12//   of conditions and the following disclaimer in the documentation and/or other materials
13//   provided with the distribution.
14//
15// - Neither the name of the ICSharpCode team nor the names of its contributors may be used to
16//   endorse or promote products derived from this software without specific prior written
17//   permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
20// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28using System;
29using System.CodeDom.Compiler;
30using System.Collections.Generic;
31using System.Diagnostics;
32using System.Drawing;
33using System.IO;
34using System.Reflection;
35using System.Threading;
36using System.Windows.Forms;
37using HeuristicLab.Common.Resources;
38using ICSharpCode.TextEditor;
39using ICSharpCode.TextEditor.Document;
40using Dom = ICSharpCode.SharpDevelop.Dom;
41using NRefactory = ICSharpCode.NRefactory;
42
43namespace HeuristicLab.CodeEditor {
44
45  public partial class CodeEditor : UserControl {
46
47    #region Fields & Properties
48
49    internal Dom.ProjectContentRegistry projectContentRegistry;
50    internal Dom.DefaultProjectContent projectContent;
51    internal Dom.ParseInformation parseInformation = new Dom.ParseInformation();
52    Dom.ICompilationUnit lastCompilationUnit;
53    Thread parserThread;
54
55    /// <summary>
56    /// Many SharpDevelop.Dom methods take a file name, which is really just a unique identifier
57    /// for a file - Dom methods don't try to access code files on disk, so the file does not have
58    /// to exist.
59    /// SharpDevelop itself uses internal names of the kind "[randomId]/Class1.cs" to support
60    /// code-completion in unsaved files.
61    /// </summary>
62    public const string DummyFileName = "edited.cs";
63
64    private IDocument Doc {
65      get {
66        return textEditor.Document;
67      }
68    }
69
70    private string prefix = "";
71    private TextMarker prefixMarker =
72      new TextMarker(0, 1, TextMarkerType.SolidBlock, Color.LightGray) {
73        IsReadOnly = true,
74      };
75    public string Prefix {
76      get {
77        return prefix;
78      }
79      set {
80        if (value == null) value = "";
81        if (prefix == value) return;
82        Doc.MarkerStrategy.RemoveMarker(prefixMarker);
83        Doc.Remove(0, prefix.Length);
84        prefix = value;
85        if (value.Length > 0) {
86          Doc.Insert(0, value);
87          prefixMarker.Offset = 0;
88          prefixMarker.Length = prefix.Length;
89          Doc.MarkerStrategy.AddMarker(prefixMarker);
90        }
91        Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
92        Doc.CommitUpdate();
93      }
94    }
95
96    private string suffix = "";
97    private TextMarker suffixMarker =
98      new TextMarker(0, 1, TextMarkerType.SolidBlock, Color.LightGray) {
99        IsReadOnly = true,
100      };
101    public string Suffix {
102      get {
103        return suffix;
104      }
105      set {
106        if (value == null) value = "";
107        if (suffix == value) return;
108        Doc.MarkerStrategy.RemoveMarker(suffixMarker);
109        Doc.Remove(Doc.TextLength - suffix.Length, suffix.Length);
110        suffix = value;
111        if (value.Length > 0) {
112          suffixMarker.Offset = Doc.TextLength;
113          Doc.Insert(Doc.TextLength, value);
114          suffixMarker.Length = suffix.Length;
115          Doc.MarkerStrategy.AddMarker(suffixMarker);
116        }
117        Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
118        Doc.CommitUpdate();
119      }
120    }
121
122    public string UserCode {
123      get {
124        return Doc.GetText(
125          prefix.Length,
126          Doc.TextLength - suffix.Length - prefix.Length);
127      }
128      set {
129        Doc.Replace(prefix.Length, Doc.TextLength - suffix.Length - prefix.Length, value);
130        Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
131        Doc.CommitUpdate();
132      }
133    }
134
135    #endregion
136
137    public event EventHandler TextEditorValidated;
138
139    protected void OnTextEditorValidated() {
140      if (TextEditorValidated != null)
141        TextEditorValidated(this, EventArgs.Empty);
142    }
143
144    public CodeEditor() {
145      InitializeComponent();
146
147      textEditor.ActiveTextAreaControl.TextEditorProperties.SupportReadOnlySegments = true;
148
149      textEditor.SetHighlighting("C#");
150      textEditor.ShowEOLMarkers = false;
151      textEditor.ShowInvalidLines = false;
152      HostCallbackImplementation.Register(this);
153      CodeCompletionKeyHandler.Attach(this, textEditor);
154      ToolTipProvider.Attach(this, textEditor);
155
156      projectContentRegistry = new Dom.ProjectContentRegistry(); // Default .NET 2.0 registry
157      try {
158        string persistencePath = Path.Combine(Path.GetTempPath(), "HeuristicLab.CodeEditor");
159        if (!Directory.Exists(persistencePath))
160          Directory.CreateDirectory(persistencePath);
161        FileStream fs = File.Create(Path.Combine(persistencePath, "test.tmp"));
162        fs.Close();
163        File.Delete(Path.Combine(persistencePath, "test.tmp"));
164        // if we made it this far, enable on-disk parsing cache
165        projectContentRegistry.ActivatePersistence(persistencePath);
166      }
167      catch (NotSupportedException) {
168      }
169      catch (IOException) {
170      }
171      catch (System.Security.SecurityException) {
172      }
173      catch (UnauthorizedAccessException) {
174      }
175      catch (ArgumentException) {
176      }
177      projectContent = new Dom.DefaultProjectContent();
178      projectContent.Language = Dom.LanguageProperties.CSharp;
179    }
180
181    protected override void OnLoad(EventArgs e) {
182      base.OnLoad(e);
183
184      if (DesignMode)
185        return;
186
187      textEditor.ActiveTextAreaControl.TextArea.KeyEventHandler += new ICSharpCode.TextEditor.KeyEventHandler(TextArea_KeyEventHandler);
188      textEditor.ActiveTextAreaControl.TextArea.DoProcessDialogKey += new DialogKeyProcessor(TextArea_DoProcessDialogKey);
189
190      parserThread = new Thread(ParserThread);
191      parserThread.IsBackground = true;
192      parserThread.Start();
193
194      textEditor.Validated += (s, a) => { OnTextEditorValidated(); };
195      InitializeImageList();
196    }
197
198    private void InitializeImageList() {
199      imageList1.Images.Clear();
200      imageList1.Images.Add("Icons.16x16.Class.png", VS2008ImageLibrary.Class);
201      imageList1.Images.Add("Icons.16x16.Method.png", VS2008ImageLibrary.Method);
202      imageList1.Images.Add("Icons.16x16.Property.png", VS2008ImageLibrary.Properties);
203      imageList1.Images.Add("Icons.16x16.Field.png", VS2008ImageLibrary.Field);
204      imageList1.Images.Add("Icons.16x16.Enum.png", VS2008ImageLibrary.Enum);
205      imageList1.Images.Add("Icons.16x16.NameSpace.png", VS2008ImageLibrary.Namespace);
206      imageList1.Images.Add("Icons.16x16.Event.png", VS2008ImageLibrary.Event);
207    }
208
209    #region keyboard handlers: filter input in read-only areas
210
211    bool TextArea_KeyEventHandler(char ch) {
212      int caret = textEditor.ActiveTextAreaControl.Caret.Offset;
213      return caret < prefix.Length || caret > Doc.TextLength - suffix.Length;
214    }
215
216    bool TextArea_DoProcessDialogKey(Keys keyData) {
217      if (keyData == Keys.Return) {
218        int caret = textEditor.ActiveTextAreaControl.Caret.Offset;
219        if (caret < prefix.Length ||
220            caret > Doc.TextLength - suffix.Length) {
221          return true;
222        }
223      }
224      return false;
225    }
226
227    #endregion
228
229    public void ScrollAfterPrefix() {
230      int lineNr = prefix != null ? Doc.OffsetToPosition(prefix.Length).Line : 0;
231      textEditor.ActiveTextAreaControl.JumpTo(lineNr + 1);
232    }
233
234    private List<TextMarker> errorMarkers = new List<TextMarker>();
235    private List<Bookmark> errorBookmarks = new List<Bookmark>();
236    public void ShowCompileErrors(CompilerErrorCollection compilerErrors, string filename) {
237      Doc.MarkerStrategy.RemoveAll(m => errorMarkers.Contains(m));
238      Doc.BookmarkManager.RemoveMarks(m => errorBookmarks.Contains(m));
239      errorMarkers.Clear();
240      errorBookmarks.Clear();
241      errorLabel.Text = "";
242      errorLabel.ToolTipText = null;
243      if (compilerErrors == null)
244        return;
245      foreach (CompilerError error in compilerErrors) {
246        if (!error.FileName.EndsWith(filename)) {
247          errorLabel.Text = "error in generated code";
248          errorLabel.ToolTipText = string.Format("{0}{1}:{2} -> {3}",
249            errorLabel.ToolTipText != null ? (errorLabel.ToolTipText + "\n\n") : "",
250            error.Line, error.Column,
251            error.ErrorText);
252          continue;
253        }
254        var startPosition = Doc.OffsetToPosition(prefix.Length);
255        if (error.Line == 1)
256          error.Column += startPosition.Column;
257        error.Line += startPosition.Line;
258        AddErrorMarker(error);
259        AddErrorBookmark(error);
260      }
261      Doc.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
262      Doc.CommitUpdate();
263    }
264
265    private void AddErrorMarker(CompilerError error) {
266      var segment = GetSegmentAtOffset(error.Line, error.Column);
267      Color color = error.IsWarning ? Color.Blue : Color.Red;
268      var marker = new TextMarker(segment.Offset, segment.Length, TextMarkerType.WaveLine, color) {
269        ToolTip = error.ErrorText,
270      };
271      errorMarkers.Add(marker);
272      Doc.MarkerStrategy.AddMarker(marker);
273    }
274
275    private void AddErrorBookmark(CompilerError error) {
276      var bookmark = new ErrorBookmark(Doc, new TextLocation(error.Column, error.Line - 1));
277      errorBookmarks.Add(bookmark);
278      Doc.BookmarkManager.AddMark(bookmark);
279    }
280
281    private AbstractSegment GetSegmentAtOffset(int lineNr, int columnNr) {
282      lineNr = Math.Max(Doc.OffsetToPosition(prefix.Length).Line, lineNr);
283      lineNr = Math.Min(Doc.OffsetToPosition(Doc.TextLength - suffix.Length).Line, lineNr);
284      var line = Doc.GetLineSegment(lineNr - 1);
285      columnNr = Math.Max(0, columnNr);
286      columnNr = Math.Min(line.Length, columnNr);
287      var word = line.GetWord(columnNr);
288      AbstractSegment segment = new AbstractSegment();
289      if (word != null) {
290        segment.Offset = line.Offset + word.Offset;
291        segment.Length = word.Length;
292      } else {
293        segment.Offset = line.Offset + columnNr - 1;
294        segment.Length = 1;
295      }
296      return segment;
297    }
298
299    private HashSet<Assembly> assemblies = new HashSet<Assembly>();
300    public IEnumerable<Assembly> ReferencedAssemblies {
301      get { return assemblies; }
302    }
303    public void AddAssembly(Assembly a) {
304      ShowMessage("Loading " + a.GetName().Name + "...");
305      if (assemblies.Contains(a))
306        return;
307      var reference = projectContentRegistry.GetProjectContentForReference(a.GetName().Name, a.Location);
308      projectContent.AddReferencedContent(reference);
309      assemblies.Add(a);
310      ShowMessage("Ready");
311    }
312    public void RemoveAssembly(Assembly a) {
313      ShowMessage("Unloading " + a.GetName().Name + "...");
314      if (!assemblies.Contains(a))
315        return;
316      var content = projectContentRegistry.GetExistingProjectContent(a.Location);
317      if (content != null) {
318        projectContent.ReferencedContents.Remove(content);
319        projectContentRegistry.UnloadProjectContent(content);
320      }
321      ShowMessage("Ready");
322    }
323
324    private bool runParser = true;
325    private void ParserThread() {
326      BeginInvoke(new MethodInvoker(delegate { parserThreadLabel.Text = "Loading mscorlib..."; }));
327      projectContent.AddReferencedContent(projectContentRegistry.Mscorlib);
328      ParseStep();
329      BeginInvoke(new MethodInvoker(delegate { parserThreadLabel.Text = "Ready"; }));
330      while (runParser && !IsDisposed) {
331        ParseStep();
332        Thread.Sleep(2000);
333      }
334    }
335
336    private void ParseStep() {
337      try {
338        string code = null;
339        Invoke(new MethodInvoker(delegate { code = textEditor.Text; }));
340        TextReader textReader = new StringReader(code);
341        Dom.ICompilationUnit newCompilationUnit;
342        NRefactory.SupportedLanguage supportedLanguage;
343        supportedLanguage = NRefactory.SupportedLanguage.CSharp;
344        using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(supportedLanguage, textReader)) {
345          p.ParseMethodBodies = false;
346          p.Parse();
347          newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit);
348        }
349        projectContent.UpdateCompilationUnit(lastCompilationUnit, newCompilationUnit, DummyFileName);
350        lastCompilationUnit = newCompilationUnit;
351        parseInformation.SetCompilationUnit(newCompilationUnit);
352      }
353      catch { }
354    }
355
356    Dom.ICompilationUnit ConvertCompilationUnit(NRefactory.Ast.CompilationUnit cu) {
357      Dom.NRefactoryResolver.NRefactoryASTConvertVisitor converter;
358      converter = new Dom.NRefactoryResolver.NRefactoryASTConvertVisitor(projectContent);
359      cu.AcceptVisitor(converter, null);
360      return converter.Cu;
361    }
362
363    private void ShowMessage(string m) {
364      if (this.Handle == null)
365        return;
366      BeginInvoke(new Action(() => parserThreadLabel.Text = m));
367    }
368
369    private void toolStripStatusLabel1_Click(object sender, EventArgs e) {
370      var proc = new Process();
371      proc.StartInfo.FileName = sharpDevelopLabel.Tag.ToString();
372      proc.Start();
373    }
374
375    private void CodeEditor_Resize(object sender, EventArgs e) {
376      var textArea = textEditor.ActiveTextAreaControl.TextArea;
377      var vScrollBar = textEditor.ActiveTextAreaControl.VScrollBar;
378      var hScrollBar = textEditor.ActiveTextAreaControl.HScrollBar;
379
380      textArea.SuspendLayout();
381      textArea.Width = textEditor.Width - vScrollBar.Width;
382      textArea.Height = textEditor.Height - hScrollBar.Height;
383      textArea.ResumeLayout();
384
385      vScrollBar.SuspendLayout();
386      vScrollBar.Location = new Point(textArea.Width, 0);
387      vScrollBar.Height = textArea.Height;
388      vScrollBar.ResumeLayout();
389
390      hScrollBar.SuspendLayout();
391      hScrollBar.Location = new Point(0, textArea.Height);
392      hScrollBar.Width = textArea.Width;
393      hScrollBar.ResumeLayout();
394    }
395  }
396}
Note: See TracBrowser for help on using the repository browser.