Beispiel #1
0
        /// <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;
            }
        }