[11700] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11700] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.CodeDom.Compiler;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.ComponentModel;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using System.Threading.Tasks;
|
---|
| 28 | using System.Windows.Documents;
|
---|
| 29 | using ICSharpCode.AvalonEdit;
|
---|
| 30 | using ICSharpCode.AvalonEdit.AddIn;
|
---|
| 31 | using ICSharpCode.AvalonEdit.CodeCompletion;
|
---|
| 32 | using ICSharpCode.AvalonEdit.Document;
|
---|
[11741] | 33 | using ICSharpCode.AvalonEdit.Editing;
|
---|
[11700] | 34 | using ICSharpCode.AvalonEdit.Highlighting;
|
---|
[11804] | 35 | using ICSharpCode.AvalonEdit.Indentation.CSharp;
|
---|
[11700] | 36 | using ICSharpCode.AvalonEdit.Search;
|
---|
| 37 | using ICSharpCode.NRefactory.Editor;
|
---|
| 38 | using ICSharpCode.SharpDevelop.Editor;
|
---|
| 39 | using Forms = System.Windows.Forms;
|
---|
| 40 | using Input = System.Windows.Input;
|
---|
| 41 | using Media = System.Windows.Media;
|
---|
| 42 |
|
---|
| 43 | namespace HeuristicLab.CodeEditor {
|
---|
[12473] | 44 | public partial class CodeEditor : CodeEditorBase {
|
---|
[11700] | 45 | private static readonly Media.Color WarningColor = Media.Colors.Blue;
|
---|
| 46 | private static readonly Media.Color ErrorColor = Media.Colors.Red;
|
---|
| 47 | private static readonly Media.Color ReadOnlyColor = Media.Colors.Moccasin;
|
---|
| 48 |
|
---|
| 49 | private const string DefaultDocumentFileName = "Document";
|
---|
| 50 | private const string DefaultTextEditorSyntaxHighlighting = "C#";
|
---|
| 51 | private const string DefaultTextEditorFontFamily = "Consolas";
|
---|
| 52 | private const double DefaultTextEditorFontSize = 13.0;
|
---|
| 53 | private const bool DefaultTextEditorShowLineNumbers = true;
|
---|
| 54 | private const bool DefaultTextEditorShowSpaces = true;
|
---|
| 55 | private const bool DefaultTextEditorShowTabs = true;
|
---|
| 56 | private const bool DefaultTextEditorConvertTabsToSpaces = true;
|
---|
| 57 | private const bool DefaultTextEditorHighlightCurrentLine = true;
|
---|
| 58 | private const int DefaultTextEditorIndentationSize = 2;
|
---|
| 59 |
|
---|
| 60 | private AssemblyLoader assemblyLoader;
|
---|
| 61 | private TextMarkerService textMarkerService;
|
---|
| 62 |
|
---|
| 63 | #region Properties
|
---|
| 64 | internal TextEditor TextEditor { get { return avalonEditWrapper.TextEditor; } }
|
---|
| 65 | internal Input.RoutedCommand CompletionCommand;
|
---|
| 66 | internal CompletionWindow CompletionWindow;
|
---|
| 67 | internal OverloadInsightWindow OverloadInsightWindow;
|
---|
| 68 |
|
---|
| 69 | private TextDocument Doc { get { return TextEditor.Document; } }
|
---|
| 70 |
|
---|
| 71 | private ITextMarker prefixMarker;
|
---|
| 72 | private string prefix = string.Empty;
|
---|
[12473] | 73 | public override string Prefix {
|
---|
[11700] | 74 | get { return prefix; }
|
---|
| 75 | set {
|
---|
| 76 | if (value == null) value = string.Empty;
|
---|
| 77 | if (prefix == value) return;
|
---|
[11835] | 78 | if (prefixMarker != null) prefixMarker.Delete();
|
---|
[11700] | 79 | Doc.Remove(0, prefix.Length);
|
---|
| 80 | prefix = value;
|
---|
| 81 | if (value.Length > 0) {
|
---|
| 82 | Doc.Insert(0, prefix);
|
---|
| 83 | prefixMarker = textMarkerService.Create(0, prefix.Length);
|
---|
| 84 | prefixMarker.BackgroundColor = ReadOnlyColor;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | private ITextMarker suffixMarker;
|
---|
| 90 | private string suffix = string.Empty;
|
---|
[12473] | 91 | public override string Suffix {
|
---|
[11700] | 92 | get { return suffix; }
|
---|
| 93 | set {
|
---|
| 94 | if (value == null) value = string.Empty;
|
---|
| 95 | if (suffix == value) return;
|
---|
[11835] | 96 | if (suffixMarker != null) suffixMarker.Delete();
|
---|
[11700] | 97 | Doc.Remove(Doc.TextLength - suffix.Length, suffix.Length);
|
---|
| 98 | suffix = value;
|
---|
| 99 | if (value.Length > 0) {
|
---|
| 100 | int offset = Doc.TextLength;
|
---|
| 101 | Doc.Insert(offset, suffix);
|
---|
| 102 | suffixMarker = textMarkerService.Create(offset, suffix.Length);
|
---|
| 103 | suffixMarker.BackgroundColor = ReadOnlyColor;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[12473] | 108 | public override string UserCode {
|
---|
[11700] | 109 | get { return Doc.GetText(prefix.Length, Doc.TextLength - suffix.Length - prefix.Length); }
|
---|
| 110 | set {
|
---|
| 111 | if (Doc.Text == value) return;
|
---|
| 112 | Doc.Replace(prefix.Length, Doc.TextLength - suffix.Length - prefix.Length, value);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | #region TextEditor
|
---|
| 117 | [DefaultValue(DefaultTextEditorSyntaxHighlighting)]
|
---|
| 118 | public string TextEditorSyntaxHighlighting {
|
---|
| 119 | get { return TextEditor.SyntaxHighlighting.Name; }
|
---|
| 120 | set {
|
---|
| 121 | TextEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(value);
|
---|
| 122 | ApplyLanguageFeatures();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | [DefaultValue(DefaultTextEditorShowLineNumbers)]
|
---|
| 127 | public bool TextEditorShowLineNumbers {
|
---|
| 128 | get { return TextEditor.ShowLineNumbers; }
|
---|
| 129 | set { TextEditor.ShowLineNumbers = value; }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | [DefaultValue(DefaultTextEditorShowSpaces)]
|
---|
| 133 | public bool TextEditorShowSpaces {
|
---|
| 134 | get { return TextEditor.Options.ShowSpaces; }
|
---|
| 135 | set { TextEditor.Options.ShowSpaces = value; }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | [DefaultValue(DefaultTextEditorShowTabs)]
|
---|
| 139 | public bool TextEditorShowTabs {
|
---|
| 140 | get { return TextEditor.Options.ShowTabs; }
|
---|
| 141 | set { TextEditor.Options.ShowTabs = value; }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | [DefaultValue(DefaultTextEditorConvertTabsToSpaces)]
|
---|
| 145 | public bool TextEditorConvertTabsToSpaces {
|
---|
| 146 | get { return TextEditor.Options.ConvertTabsToSpaces; }
|
---|
| 147 | set { TextEditor.Options.ConvertTabsToSpaces = value; }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | [DefaultValue(DefaultTextEditorHighlightCurrentLine)]
|
---|
| 151 | public bool TextEditorHighlightCurrentLine {
|
---|
| 152 | get { return TextEditor.Options.HighlightCurrentLine; }
|
---|
| 153 | set { TextEditor.Options.HighlightCurrentLine = value; }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | [DefaultValue(DefaultTextEditorIndentationSize)]
|
---|
| 157 | public int TextEditorIndentationSize {
|
---|
| 158 | get { return TextEditor.Options.IndentationSize; }
|
---|
| 159 | set { TextEditor.Options.IndentationSize = value; }
|
---|
| 160 | }
|
---|
| 161 | #endregion
|
---|
| 162 |
|
---|
[12473] | 163 | public override bool ReadOnly {
|
---|
[11700] | 164 | get { return TextEditor.IsReadOnly; }
|
---|
| 165 | set { TextEditor.IsReadOnly = value; }
|
---|
| 166 | }
|
---|
| 167 | #endregion
|
---|
| 168 |
|
---|
| 169 | public CodeEditor() {
|
---|
| 170 | InitializeComponent();
|
---|
| 171 | InitializeTextEditor();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private void InitializeTextEditor() {
|
---|
| 175 | #region AssemblyLoader
|
---|
| 176 | assemblyLoader = new AssemblyLoader();
|
---|
| 177 | assemblyLoader.AssembliesLoading += (sender, args) => OnAssembliesLoading(args.Value);
|
---|
| 178 | assemblyLoader.InternalAssembliesLoaded += (sender, args) => OnInternalAssembliesLoaded(args.Value);
|
---|
| 179 | assemblyLoader.AssembliesLoaded += (sender, args) => OnAssembliesLoaded(args.Value);
|
---|
| 180 | assemblyLoader.AssembliesUnloading += (sender, args) => OnAssembliesUnloading(args.Value);
|
---|
| 181 | assemblyLoader.InternalAssembliesUnloaded += (sender, args) => OnInternalAssembliesUnloaded(args.Value);
|
---|
| 182 | assemblyLoader.AssembliesUnloaded += (sender, args) => OnAssembliesUnloaded(args.Value);
|
---|
| 183 | #endregion
|
---|
| 184 |
|
---|
| 185 | #region TextMarkerService
|
---|
| 186 | textMarkerService = new TextMarkerService(TextEditor.Document);
|
---|
| 187 | TextEditor.TextArea.TextView.BackgroundRenderers.Add(textMarkerService);
|
---|
| 188 | TextEditor.TextArea.TextView.LineTransformers.Add(textMarkerService);
|
---|
| 189 | TextEditor.TextArea.TextView.Services.AddService(typeof(ITextMarkerService), textMarkerService);
|
---|
| 190 | #endregion
|
---|
| 191 |
|
---|
| 192 | #region ReadOnlySectionProvider
|
---|
| 193 | TextEditor.TextArea.ReadOnlySectionProvider = new MethodDefinitionReadOnlySectionProvider(this);
|
---|
| 194 | #endregion
|
---|
| 195 |
|
---|
| 196 | #region SearchPanel
|
---|
| 197 | SearchPanel.Install(TextEditor);
|
---|
| 198 | #endregion
|
---|
| 199 |
|
---|
| 200 | #region CompletionCommand
|
---|
| 201 | CompletionCommand = new Input.RoutedCommand();
|
---|
| 202 | CompletionCommand.InputGestures.Add(new Input.KeyGesture(Input.Key.Space, Input.ModifierKeys.Control));
|
---|
| 203 | #endregion
|
---|
| 204 |
|
---|
[11741] | 205 | #region MoveLinesUpCommand
|
---|
| 206 | var moveLinesUpCommand = new Input.RoutedCommand();
|
---|
| 207 | moveLinesUpCommand.InputGestures.Add(new Input.KeyGesture(Input.Key.Up, Input.ModifierKeys.Alt));
|
---|
[11785] | 208 | var moveLinesUpCommandBinding = new Input.CommandBinding(moveLinesUpCommand, (sender, args) => ExecuteMoveLinesCommand(MovementDirection.Up));
|
---|
[11741] | 209 | TextEditor.CommandBindings.Add(moveLinesUpCommandBinding);
|
---|
| 210 | #endregion
|
---|
| 211 |
|
---|
| 212 | #region MoveLinesDownCommand
|
---|
| 213 | var moveLinesDownCommand = new Input.RoutedCommand();
|
---|
| 214 | moveLinesDownCommand.InputGestures.Add(new Input.KeyGesture(Input.Key.Down, Input.ModifierKeys.Alt));
|
---|
[11785] | 215 | var moveLinesDownCommandBinding = new Input.CommandBinding(moveLinesDownCommand, (sender, args) => ExecuteMoveLinesCommand(MovementDirection.Down));
|
---|
[11741] | 216 | TextEditor.CommandBindings.Add(moveLinesDownCommandBinding);
|
---|
| 217 | #endregion
|
---|
| 218 |
|
---|
[11785] | 219 | #region GoToLineCommand
|
---|
| 220 | var goToLineCommand = new Input.RoutedCommand();
|
---|
| 221 | goToLineCommand.InputGestures.Add(new Input.KeyGesture(Input.Key.G, Input.ModifierKeys.Control));
|
---|
| 222 | var goToLineCommandBinding = new Input.CommandBinding(goToLineCommand, (sender, args) => ExecuteGoToLineCommand());
|
---|
| 223 | TextEditor.CommandBindings.Add(goToLineCommandBinding);
|
---|
| 224 | #endregion
|
---|
| 225 |
|
---|
[11700] | 226 | TextEditorSyntaxHighlighting = DefaultTextEditorSyntaxHighlighting;
|
---|
| 227 | TextEditorShowLineNumbers = DefaultTextEditorShowLineNumbers;
|
---|
| 228 | TextEditorShowSpaces = DefaultTextEditorShowSpaces;
|
---|
| 229 | TextEditorShowTabs = DefaultTextEditorShowTabs;
|
---|
| 230 | TextEditorConvertTabsToSpaces = DefaultTextEditorConvertTabsToSpaces;
|
---|
| 231 | TextEditorHighlightCurrentLine = DefaultTextEditorHighlightCurrentLine;
|
---|
| 232 | TextEditorIndentationSize = DefaultTextEditorIndentationSize;
|
---|
| 233 |
|
---|
| 234 | Doc.FileName = DefaultDocumentFileName;
|
---|
| 235 |
|
---|
| 236 | TextEditor.FontFamily = new Media.FontFamily(DefaultTextEditorFontFamily);
|
---|
| 237 | TextEditor.FontSize = DefaultTextEditorFontSize;
|
---|
[11743] | 238 | TextEditor.Options.EnableVirtualSpace = true;
|
---|
[11804] | 239 | TextEditor.TextArea.IndentationStrategy = new CSharpIndentationStrategy(TextEditor.Options);
|
---|
[11700] | 240 |
|
---|
| 241 | TextEditor.TextChanged += (sender, args) => {
|
---|
[11835] | 242 | foreach (var marker in textMarkerService.TextMarkers) {
|
---|
| 243 | if (marker == prefixMarker || marker == suffixMarker) continue;
|
---|
| 244 | if (marker.Length != (int)marker.Tag)
|
---|
| 245 | marker.Delete();
|
---|
[11836] | 246 | else {
|
---|
| 247 | int caretOffset = TextEditor.CaretOffset;
|
---|
| 248 | var line = Doc.GetLineByOffset(marker.StartOffset);
|
---|
| 249 | int lineEndOffset = line.EndOffset;
|
---|
| 250 | if (caretOffset == lineEndOffset) // special case for markers beyond line length
|
---|
| 251 | marker.Delete();
|
---|
| 252 | }
|
---|
[11835] | 253 | }
|
---|
[11700] | 254 | OnTextEditorTextChanged();
|
---|
| 255 | };
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | #region Assembly Management
|
---|
[12473] | 259 | public override void AddAssembly(Assembly a) {
|
---|
[11700] | 260 | assemblyLoader.AddAssembly(a);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[12473] | 263 | public override void AddAssemblies(IEnumerable<Assembly> assemblies) {
|
---|
[11700] | 264 | assemblyLoader.AddAssemblies(assemblies);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[12473] | 267 | public override async Task AddAssembliesAsync(IEnumerable<Assembly> assemblies) {
|
---|
[11700] | 268 | await assemblyLoader.AddAssembliesAsync(assemblies);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[12473] | 271 | public override void RemoveAssembly(Assembly a) {
|
---|
[11700] | 272 | assemblyLoader.RemoveAssembly(a);
|
---|
| 273 | }
|
---|
| 274 | #endregion
|
---|
| 275 |
|
---|
[12473] | 276 | public override void ScrollToPosition(int line, int column) {
|
---|
[11700] | 277 | var segment = GetSegmentAtLocation(line, column);
|
---|
| 278 | TextEditor.CaretOffset = segment.Offset + segment.Length;
|
---|
| 279 | TextEditor.ScrollToLine(line);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[12473] | 282 | public override void ScrollAfterPrefix() {
|
---|
[11700] | 283 | var location = Doc.GetLocation(prefix.Length);
|
---|
| 284 | ScrollToPosition(location.Line, location.Column);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[11741] | 287 | private enum MovementDirection { Up, Down }
|
---|
[11785] | 288 | private void ExecuteMoveLinesCommand(MovementDirection movementDirection) {
|
---|
[11741] | 289 | var textArea = TextEditor.TextArea;
|
---|
| 290 | var selection = textArea.Selection;
|
---|
[11743] | 291 | var caret = textArea.Caret;
|
---|
[11741] | 292 |
|
---|
[11743] | 293 | var selectionStartPosition = selection.StartPosition;
|
---|
| 294 | var selectionEndPosition = selection.EndPosition;
|
---|
| 295 | var caretPosition = caret.Position;
|
---|
| 296 | int caretOffset = caret.Offset;
|
---|
| 297 |
|
---|
| 298 | bool advancedPositionCalcualtion = selection is RectangleSelection || !selection.IsEmpty;
|
---|
| 299 |
|
---|
| 300 | int selectionStartOffset, selectionEndOffset;
|
---|
| 301 | if (advancedPositionCalcualtion) {
|
---|
| 302 | if (selectionStartPosition.CompareTo(selectionEndPosition) > 0) {
|
---|
| 303 | var temp = selectionStartPosition;
|
---|
| 304 | selectionStartPosition = selectionEndPosition;
|
---|
| 305 | selectionEndPosition = temp;
|
---|
| 306 | }
|
---|
| 307 | selectionStartOffset = Doc.GetOffset(selectionStartPosition.Location);
|
---|
| 308 | selectionEndOffset = Doc.GetOffset(selectionEndPosition.Location);
|
---|
[11741] | 309 | } else {
|
---|
[11743] | 310 | selectionStartOffset = selectionEndOffset = TextEditor.SelectionStart;
|
---|
[11741] | 311 | }
|
---|
| 312 |
|
---|
| 313 | int selectionLength = selection.Length;
|
---|
| 314 |
|
---|
[11743] | 315 | var startLine = Doc.GetLineByOffset(selectionStartOffset);
|
---|
| 316 | var endLine = Doc.GetLineByOffset(selectionEndOffset);
|
---|
[11741] | 317 |
|
---|
[11743] | 318 | if (selection.IsMultiline && selectionEndOffset == endLine.Offset) {
|
---|
| 319 | if (movementDirection == MovementDirection.Down && endLine.TotalLength == 0) return;
|
---|
| 320 | endLine = endLine.PreviousLine;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[11741] | 323 | if (movementDirection == MovementDirection.Up && startLine.LineNumber == 1) return;
|
---|
| 324 | if (movementDirection == MovementDirection.Down && endLine.LineNumber == Doc.LineCount) return;
|
---|
| 325 |
|
---|
| 326 | int startOffset = startLine.Offset;
|
---|
| 327 | string primaryText = Doc.GetText(startOffset, endLine.EndOffset - startOffset);
|
---|
| 328 | string primaryDelimiter = Doc.GetText(endLine.EndOffset, endLine.DelimiterLength);
|
---|
| 329 |
|
---|
| 330 | var secondaryLine = movementDirection == MovementDirection.Up ? startLine.PreviousLine : endLine.NextLine;
|
---|
| 331 | string secondaryText = Doc.GetText(secondaryLine.Offset, secondaryLine.Length);
|
---|
| 332 | string secondaryDelimiter = Doc.GetText(secondaryLine.EndOffset, secondaryLine.DelimiterLength);
|
---|
| 333 |
|
---|
[11743] | 334 | if (string.IsNullOrEmpty(primaryText + primaryDelimiter) || string.IsNullOrEmpty(secondaryText + secondaryDelimiter)) return;
|
---|
| 335 |
|
---|
[11741] | 336 | if (movementDirection == MovementDirection.Up) {
|
---|
| 337 | string replacementText = primaryText + secondaryDelimiter + secondaryText + primaryDelimiter;
|
---|
| 338 | Doc.Replace(secondaryLine.Offset, replacementText.Length, replacementText);
|
---|
| 339 | int correctionLength = secondaryText.Length + secondaryDelimiter.Length;
|
---|
[11743] | 340 | selectionStartOffset -= correctionLength;
|
---|
[11741] | 341 | caretOffset -= correctionLength;
|
---|
| 342 | } else {
|
---|
| 343 | string replacementText = secondaryText + primaryDelimiter + primaryText + secondaryDelimiter;
|
---|
| 344 | Doc.Replace(startLine.Offset, replacementText.Length, replacementText);
|
---|
| 345 | int correctionLength = secondaryText.Length + primaryDelimiter.Length;
|
---|
[11743] | 346 | selectionStartOffset += correctionLength;
|
---|
[11741] | 347 | caretOffset += correctionLength;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[11743] | 350 | if (advancedPositionCalcualtion) {
|
---|
| 351 | var newSelectionStartLocation = Doc.GetLocation(selectionStartOffset);
|
---|
| 352 | int selectionLineOffset = newSelectionStartLocation.Line - Math.Min(selectionStartPosition.Line, selectionEndPosition.Line);
|
---|
| 353 | selectionStartPosition.Line += selectionLineOffset;
|
---|
| 354 | selectionEndPosition.Line += selectionLineOffset;
|
---|
| 355 | if (selectionEndPosition.Line > Doc.LineCount) {
|
---|
| 356 | var newLocation = Doc.GetLocation(Doc.TextLength);
|
---|
| 357 | selectionEndPosition.Line = newLocation.Line;
|
---|
| 358 | selectionEndPosition.Column = newLocation.Column;
|
---|
| 359 | selectionEndPosition.VisualColumn = newLocation.Column - 1;
|
---|
| 360 | selectionEndPosition.IsAtEndOfLine = selectionStartPosition.IsAtEndOfLine; // actual value does not matter; needed for comparison
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | if (selectionStartPosition == selectionEndPosition)
|
---|
| 364 | textArea.ClearSelection();
|
---|
| 365 | else {
|
---|
| 366 | if (selection is RectangleSelection)
|
---|
| 367 | textArea.Selection = new RectangleSelection(textArea, selectionStartPosition, selectionEndPosition);
|
---|
| 368 | else
|
---|
| 369 | textArea.Selection = new SimpleSelection(textArea, selectionStartPosition, selectionEndPosition);
|
---|
| 370 | }
|
---|
[11741] | 371 | } else {
|
---|
[11743] | 372 | TextEditor.SelectionStart = selectionStartOffset;
|
---|
[11741] | 373 | TextEditor.SelectionLength = selectionLength;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[11743] | 376 | var newCaretLocation = Doc.GetLocation(Math.Min(caretOffset, Doc.TextLength));
|
---|
| 377 | var newCaretPosition = new TextViewPosition(newCaretLocation);
|
---|
| 378 | if (caretPosition.VisualColumn > caretPosition.Column) {
|
---|
| 379 | newCaretPosition.VisualColumn = caretPosition.VisualColumn;
|
---|
| 380 | }
|
---|
| 381 | caret.Position = newCaretPosition;
|
---|
[11741] | 382 | }
|
---|
| 383 |
|
---|
[11785] | 384 | private void ExecuteGoToLineCommand() {
|
---|
| 385 | using (var dlg = new GoToLineDialog(TextEditor)) {
|
---|
| 386 | var result = dlg.ShowDialog(this);
|
---|
| 387 | if (result == Forms.DialogResult.OK) {
|
---|
| 388 | int lineNumber = dlg.Line;
|
---|
| 389 | var line = Doc.GetLineByNumber(lineNumber);
|
---|
| 390 | int offset = line.Offset;
|
---|
| 391 | if (TextUtilities.GetLeadingWhitespace(Doc, line).Length > 0)
|
---|
[11825] | 392 | offset = TextUtilities.GetNextCaretPosition(Doc, offset, LogicalDirection.Forward, CaretPositioningMode.WordBorder);
|
---|
[11785] | 393 | TextEditor.CaretOffset = offset;
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[11700] | 398 | #region Compiler Errors
|
---|
[12473] | 399 | public override void ShowCompileErrors(CompilerErrorCollection compilerErrors) {
|
---|
[11700] | 400 | if (compilerErrors == null) return;
|
---|
| 401 |
|
---|
| 402 | textMarkerService.RemoveAll(x => x != prefixMarker && x != suffixMarker);
|
---|
| 403 |
|
---|
| 404 | foreach (CompilerError error in compilerErrors) {
|
---|
| 405 | var startLocation = Doc.GetLocation(prefix.Length);
|
---|
| 406 | if (error.Line == 1) error.Column += startLocation.Column;
|
---|
| 407 | error.Line += startLocation.Line;
|
---|
| 408 | AddErrorMarker(error);
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | private void AddErrorMarker(CompilerError error) {
|
---|
| 413 | var segment = GetSegmentAtLocation(error.Line, error.Column);
|
---|
| 414 | var marker = textMarkerService.Create(segment.Offset, segment.Length);
|
---|
| 415 | marker.MarkerTypes = TextMarkerTypes.SquigglyUnderline;
|
---|
| 416 | marker.MarkerColor = error.IsWarning ? WarningColor : ErrorColor;
|
---|
[11835] | 417 | marker.Tag = segment.Length;
|
---|
[11700] | 418 | }
|
---|
| 419 |
|
---|
| 420 | private ISegment GetSegmentAtLocation(int line, int column) {
|
---|
| 421 | line = Math.Max(Doc.GetLocation(prefix.Length).Line, line - 1);
|
---|
| 422 | line = Math.Min(Doc.GetLocation(Doc.TextLength - suffix.Length).Line, line);
|
---|
| 423 |
|
---|
| 424 | var startOffset = Doc.GetOffset(line, column);
|
---|
| 425 | var lineEndOffset = Doc.GetLineByNumber(line).EndOffset;
|
---|
| 426 | var endOffset = TextUtilities.GetNextCaretPosition(Doc, startOffset, LogicalDirection.Forward, CaretPositioningMode.WordBorder);
|
---|
| 427 | if (endOffset < 0) endOffset = startOffset;
|
---|
| 428 | endOffset = Math.Min(lineEndOffset + 1, endOffset);
|
---|
| 429 |
|
---|
| 430 | var segment = new TextSegment { StartOffset = startOffset, EndOffset = endOffset };
|
---|
| 431 |
|
---|
| 432 | return segment;
|
---|
| 433 | }
|
---|
| 434 | #endregion
|
---|
| 435 |
|
---|
| 436 | private void ApplyLanguageFeatures() {
|
---|
| 437 | switch (TextEditorSyntaxHighlighting) {
|
---|
| 438 | case "XML":
|
---|
[11933] | 439 | XmlLanguageFeatures.Apply(this);
|
---|
[11700] | 440 | break;
|
---|
| 441 | default:
|
---|
[11933] | 442 | CSharpLanguageFeatures.Apply(this);
|
---|
[11700] | 443 | break;
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 | }
|
---|
| 447 | }
|
---|