/// <summary> /// Initializes important members of this application. /// </summary> /// <param name="mainWindow"> /// A static instance of <see cref="MainWindow"/> which will be provided for other classes. /// </param> /// <param name="scheduler"> /// A <see cref="TaskScheduler"/> used to start the start <see cref="Task"/>s. /// </param> /// <exception cref="ArgumentNullException"> /// The parameter was passed as null. /// </exception> public static async void StartAsync(MainWindow mainWindow, TaskScheduler scheduler) { if (mainWindow == null) { throw new ArgumentNullException("mainWindow"); } Io.CreateFiles(); var loadFilesTask = LoadFilesAsync(); MainWindow = mainWindow; // ToDo: C# 6.0 auto-initialization of properties NoTextChangedPlox = false; cancellationToken = new CancellationTokenSource(); highlightSyntaxTask = new Task(() => FacilitateCoding.HighlightSyntaxAsync(RecognitionEngine.AllWordsInCode, CurrentProgrammingLanguage), CancellationToken.None, TaskCreationOptions.LongRunning); pseudoSenseTask = new Task(() => FacilitateCoding.PseudoSenseAsync(RecognitionEngine.PreviousWord, RecognitionEngine.AllWordsInCode, CurrentProgrammingLanguage, MainWindow.CodeListBox), CancellationToken.None, TaskCreationOptions.LongRunning); errorSearchTask = new Task( () => MistakeEngine.MistakeSearchAsync( RecognitionEngine.AllWordsInCode, mainWindow.ErrorListView, TaskScheduler.FromCurrentSynchronizationContext()), CancellationToken.None, TaskCreationOptions.LongRunning); await Task.Factory.StartNew(() => Gui.FillProgrammingLanguageComboBoxAsync(ProgrammingLanguages, MainWindow.ProgrammingLanguageComboBox), CancellationToken.None, TaskCreationOptions.None, scheduler); await loadFilesTask.ContinueWith(task => Gui.FillLocalizationLanguageComboBoxAsync(LocalizationLanguages, MainWindow.LocalizationLanguageComboBox), scheduler); MainWindow.LocalizationLanguageComboBox.SelectedItem = currentLocalizationLanguage["language"]; MainWindow.ProgrammingLanguageComboBox.SelectedItem = currentProgrammingLanguage.Name; mainWindow.Show(); mainWindow.CodeTextBox.Focus(); }
/// <summary> /// Starts the fast operations to be executed at every user interaction with the <see cref="RichTextBox"/>. /// </summary> /// <param name="richTextBox"> /// The <see cref="RichTextBox"/> which text was changed. /// </param> /// <param name="eventArgs"> /// The <see cref="TextChangedEventArgs"/> containing specific information about the user interaction. /// </param> /// <param name="scheduler"> /// The <see cref="TaskScheduler"/> containing GUI-context. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// One or all of the parameters was/ were passed as null. /// </exception> public static async Task CodeAlteredAsync(RichTextBox richTextBox, TextChangedEventArgs eventArgs, TaskScheduler scheduler) { if (richTextBox == null) { throw new ArgumentNullException("richTextBox"); } if (eventArgs == null) { throw new ArgumentNullException("eventArgs"); } if (scheduler == null) { throw new ArgumentNullException("scheduler"); } if (NoTextChangedPlox) { return; } var text = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text.Replace( " ", string.Empty); if (richTextBox.Document == null || text == "\r\n") { RecognitionEngine.AllWordsInCode.Clear(); MistakeEngine.Mistakes.Clear(); MainWindow.ErrorListView.Items.Clear(); return; } TextChange textChange = null; var textChanges = eventArgs.Changes; if (textChanges != null && textChanges.Count > 0) { textChange = textChanges.First(); } if (textChange == null || (textChange.AddedLength <= 0 && textChange.RemovedLength <= 0)) { return; } IEnumerable <Word> changedWords = RecognitionEngine.RecognizeWordsInCode(richTextBox.CaretPosition, CurrentProgrammingLanguage).Result; var newWord = changedWords.First(); if (newWord.Content == string.Empty) { await FacilitateCoding.PseudoSenseAsync(newWord, RecognitionEngine.AllWordsInCode, CurrentProgrammingLanguage, MainWindow.CodeListBox); return; } switch (pseudoSenseTask.Status) { case TaskStatus.Created: pseudoSenseTask.Start(scheduler); break; case TaskStatus.RanToCompletion: case TaskStatus.Faulted: pseudoSenseTask.Dispose(); pseudoSenseTask = new Task( () => FacilitateCoding.PseudoSenseAsync(newWord, RecognitionEngine.AllWordsInCode, CurrentProgrammingLanguage, MainWindow.CodeListBox), cancellationToken.Token, TaskCreationOptions.LongRunning); pseudoSenseTask.Start(scheduler); break; } }