/// <summary> /// Handler for removed text by other editor /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void my_RemovedText(object sender, EditedTextEventArgs e) { if (e.File == filename) { lock (CoProNetwork.locker) { while (e.Seq != cb.ExpectedSequence)//if excpected id is the id i got { System.Threading.Monitor.Wait(CoProNetwork.locker); Debug.WriteLine("Recieved seq : " + e.Seq + " Expected seq : " + cb.ExpectedSequence); } trackDict[e.Editor] = m_textView.TextSnapshot.CreateTrackingPoint(e.Location, PointTrackingMode.Positive); uiDisp.Invoke(new Action(() => { ITextEdit edit = m_textView.TextBuffer.CreateEdit(); var curTrackPoint = trackDict[e.Editor]; edit.Delete(e.Location, int.Parse(e.Command.Split(';')[1])); mySideCalling = false; edit.Apply(); edit.Dispose(); })); System.Threading.Monitor.PulseAll(CoProNetwork.locker); } } }
public async Task InsertAsync(string text) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(CancelAllToken); _textEdit.Insert(_position, text); _textEdit.Apply(); _textEdit.Dispose(); }
public void Dispose() { m_edit.Dispose(); }
protected override bool Execute(VSConstants.VSStd2KCmdID command, uint options, IntPtr pvaIn, IntPtr pvaOut) { ThreadHelper.ThrowIfNotOnUIThread(); lastToken = new Builder.Node { line = 0 }; currentLevelBrackets = new Stack <Builder.Node>(); bracketsStack = new Stack <Stack <Builder.Node> >(); edit = TextView.TextBuffer.CreateEdit(); wasPoped = false; switch (MplPackage.Options.LineEndings) { case Options.LineEnding.Unix: leaveAsIs = false; lineEnding = "\n"; break; case Options.LineEnding.Windows: leaveAsIs = false; lineEnding = "\r\n"; break; case Options.LineEnding.Document: leaveAsIs = true; break; } if (TextView.Options.IsConvertTabsToSpacesEnabled()) { indentationCharacter = ' '; indentationSize = TextView.Options.GetIndentSize(); } else { indentationCharacter = '\t'; indentationSize = 1; } selectionBegin = 0; selectionEnd = TextView.TextBuffer.CurrentSnapshot.Length; rawSource = TextView.TextBuffer.CurrentSnapshot.GetText(); caretPoint = TextView.Caret.Position.Point.GetPoint(TextView.TextBuffer, TextView.Caret.Position.Affinity); switch (command) { case VSConstants.VSStd2KCmdID.FORMATDOCUMENT: break; case VSConstants.VSStd2KCmdID.FORMATSELECTION: if (!TextView.Selection.IsEmpty && caretPoint != null) { selectionBegin = TextView.Selection.Start.Position.Position; selectionEnd = TextView.Selection.End.Position.Position; } else if (TextView.Selection.IsEmpty && caretPoint != null) { var line = TextView.TextBuffer.CurrentSnapshot.GetLineFromPosition(caretPoint.Value.Position); selectionBegin = line.Start.Position; selectionEnd = line.End.Position; } break; } GetFormattedProgram(); if (valid) { edit.Apply(); } edit.Dispose(); return(true); }
private async Task ExecuteAsync(object sender, EventArgs e) { _logger.Info("Starting command of adding translation."); var textManager = (VsTextManager)await _serviceProvider.GetServiceAsync(typeof(SVsTextManager)); Assumes.Present(textManager); var componentModel = (SComponentModel)await _serviceProvider.GetServiceAsync(typeof(SComponentModel)); Assumes.Present(componentModel); IVsTextView view; IWpfTextView wpfTextView; ITextEdit edit = null; try { int result = (textManager as IVsTextManager2).GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out view); _logger.Debug("Successfully created " + nameof(IVsTextView)); // Cast IVsTextView to IWpfTextView, so we can modify text of the file. wpfTextView = (componentModel as IComponentModel).GetService <IVsEditorAdaptersFactoryService>().GetWpfTextView(view); _logger.Debug("Successfully created " + nameof(IWpfTextView)); // IMPORTANT MOMENT: here we create ITextEdit object, which, in case of a failure, // may hang whole visual studio (thus, preventing from saving unsaved work, // see catch(InvalidOperationException)). edit = wpfTextView.TextBuffer.CreateEdit(); _logger.Info($"Successfully executed {nameof(wpfTextView.TextBuffer.CreateEdit)} and created {nameof(ITextEdit)} object."); IVsHierarchy hierarchy = null; string csProjPath = null; var selection = wpfTextView.Selection; var startPosition = selection.Start.Position.Position; var endPosition = selection.End.Position.Position; var span = new Span(startPosition, endPosition - startPosition); var textToReplace = wpfTextView.TextBuffer.CurrentSnapshot.GetText(span).Trim(' ').Trim('"'); var translationWindow = new AddTranslationWindow(); var vm = new AddTranslationViewModel(new ProjectItemsFactory(_serviceProvider), translationWindow); translationWindow.DataContext = vm; translationWindow.ShowDialog(); if (translationWindow.DialogResult != null && !translationWindow.DialogResult.Value) { _logger.Info("Anulowano dodawanie tłumaczenia!"); return; } #warning TO UNCOMMENT // edit.Replace(span, translationWindow.TranslationName); edit.Apply(); } catch (InvalidOperationException ioe) { MessageBox.Show("This should not happen, but it happened. Please, do not click anything," + " just save the file in order to not loose any work, then close the file.\nError:\n" + ioe); _logger.Fatal("Fatal exception during translation command..", ioe); if (ioe.InnerException != null) { _logger.Fatal("Fatal inner exception.", ioe); } return; } catch (Exception ex) { MessageBox.Show($"The translation extension reported an exception:\n{ex}"); _logger.Error("Exception during translation command.", ex); if (ex.InnerException != null) { _logger.Info(ex.InnerException.ToString()); } return; } finally { edit?.Dispose(); } }