EnvDTE.Document.Object(string)

Here are the examples of the csharp api class EnvDTE.Document.Object(string) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

28 Examples 7

1. Example

Project: codemaid
Source File: DocumentExtensions.cs
View license
internal static TextDocument GetTextDocument(this Document document)
        {
            return document.Object("TextDocument") as TextDocument;
        }

2. Example

View license
private string GetCurrentText()
        {
            var dte = (DTE)GetService(typeof(DTE));

            if (dte.ActiveDocument == null)
            {
                return null;
            }

            var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            if (null == doc)
            {
                return null;
            }

            return doc.Selection.Text;
        }

3. Example

Project: CodeContracts
Source File: ContextHelper.cs
View license
public static bool TryGetSelectionPoint(IServiceProvider serviceProvider, out VirtualPoint point)
    {
      Contract.Requires(serviceProvider != null);
      Contract.Ensures(Contract.ValueAtReturn(out point) != null || !Contract.Result<bool>());

      point = null;

      var dte = VsServiceProviderHelper.GetService<DTE>(serviceProvider);

      if (dte == null)
      {
        return false;
      }

      try
      {
        // code window supported only
        Document document = dte.ActiveDocument;
        if (document != null)
        {
          TextDocument tdoc = document.Object("TextDocument") as TextDocument;
          if (tdoc != null)
          {
            try
            {
              Contract.Assume(tdoc.Selection != null);
              point = tdoc.Selection.TopPoint;
            }
            catch (COMException)
            { }
          }
        }
      }
      catch (ArgumentException) { } // reported by user
      catch (COMException) { }

      return point != null;
    }

4. Example

View license
private void SetCurrentText(string newText)
        {
            var dte = (DTE)GetService(typeof(DTE));

            if (dte.ActiveDocument == null)
            {
                return;
            }

            var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            if (null == doc)
            {
                return;
            }

            doc.Selection.Text = newText;
        }

5. Example

Project: TSVN
Source File: TSVNPackage.cs
View license
private void BlameCommand(object sender, EventArgs e)
        {
            _currentFilePath = FileHelper.GetPath();
            var currentLineIndex = ((TextDocument) Dte.ActiveDocument?.Object(string.Empty))?.Selection.CurrentLine ?? 0;  
            if (string.IsNullOrEmpty(_currentFilePath)) return;
            CommandHelper.StartProcess(_tortoiseProc, $"/command:blame /path:\"{_currentFilePath}\" /line:{currentLineIndex}");
        }

6. Example

Project: vcc
Source File: VSIntegration.cs
View license
public static void InsertIntoCurrentDocument(string str)
    {
        var textDocument = DTE.ActiveDocument.Object(null) as TextDocument;
        if (textDocument != null)
        {
            textDocument.Selection.Insert(str);
        }
    }

7. Example

View license
private void MyDocumentEvents_DocumentOpened(Document Document)
        {
            if (Document.FullName.EndsWith(".sql"))
            {
                var textDoc = ((TextDocument)Document.Object());
                textDoc.ReplacePattern("/*designTime", "-- designTime");
                textDoc.ReplacePattern("endDesignTime*/", "-- endDesignTime");
                // backward compatibility
                textDoc.ReplacePattern("--designTime", "-- designTime");
                textDoc.ReplacePattern("--endDesignTime", "-- endDesignTime");


                // never got close to working. cost me 50 points on stack. just saw it open the window????
                //try
                //{
                //    if (_dte.Commands.Item("SQL.TSqlEditorConnect") != null && _dte.Commands.Item("SQL.TSqlEditorConnect").IsAvailable)
                //    {
                //        _dte.ExecuteCommand("SQL.TSqlEditorConnect");
                //    }
                //    //_dte.ExecuteCommand("SQL.TSqlEditorConnect", "Data Source=not-mobility;Initial Catalog=NORTHWND;Integrated Security=SSPI;");
                //}
                //catch (Exception ex) { }


            }
        }

8. Example

Project: Wax
Source File: DteExtensions.cs
View license
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
        private static void SetContent([NotNull] EnvDTE.Document document, [CanBeNull] string text)
        {
            var textDocument = (EnvDTE.TextDocument)document.Object("TextDocument");

            textDocument.StartPoint.CreateEditPoint().ReplaceText(textDocument.EndPoint, text, 0);
        }

9. Example

Project: CodeNav
Source File: DocumentHelper.cs
View license
public static string GetText(Document document)
        {
            var text = string.Empty;
            try
            {
                var textDocument = (TextDocument)document.Object("TextDocument");
                var startPoint = textDocument?.StartPoint?.CreateEditPoint();

                if (startPoint == null)
                {
                    LogHelper.Log("Error during mapping: Unable to find TextDocument StartPoint");
                    return null;
                };

                text = startPoint.GetText(textDocument.EndPoint);
            }
            catch (COMException e)
            {
                // Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
                // We were unable to get a start- or endpoint for the document. 
                // This was our last resort to map this document, nothing we can do further.
            }
            catch (Exception e)
            {
                LogHelper.Log("Error getting Text for document", e);
            }
            return text;
        }

10. Example

Project: ResXResourceManager
Source File: Refactorings.cs
View license
[CanBeNull]
        private static Selection GetSelection([NotNull] EnvDTE.Document document)
        {
            Contract.Requires(document != null);

            var textDocument = (EnvDTE.TextDocument)document.Object(@"TextDocument");

            var topPoint = textDocument?.Selection?.TopPoint;
            if (topPoint == null)
                return null;

            var line = textDocument.CreateEditPoint()?.GetLines(topPoint.Line, topPoint.Line + 1);
            if (line == null)
                return null;

            var fileCodeModel = document.ProjectItem?.FileCodeModel;

            return new Selection(textDocument, line, fileCodeModel);
        }

11. Example

View license
[ContractVerification(false)]
        private static bool TrySetContent([CanBeNull] EnvDTE.Document document, [CanBeNull] XDocument value)
        {
            try
            {
                var textDocument = (EnvDTE.TextDocument)document?.Object(@"TextDocument");
                if (textDocument == null)
                    return false;

                var text = value.Declaration + Environment.NewLine + value;

                textDocument.CreateEditPoint().ReplaceText(textDocument.EndPoint, text, 0);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

12. Example

View license
[CanBeNull]
        [ContractVerification(false)]
        private static XDocument TryGetContent([CanBeNull] EnvDTE.Document document)
        {
            try
            {
                var textDocument = (EnvDTE.TextDocument)document?.Object(@"TextDocument");
                var text = textDocument?.CreateEditPoint().GetText(textDocument.EndPoint);

                return text == null ? null : XDocument.Parse(text);
            }
            catch (Exception)
            {
                return null;
            }
        }

13. Example

Project: SSDT-DevPack
Source File: HighlightWordTagger.cs
View license
private string GetCurrentDocumentText(DTE dte)
        {

            

            if (dte.ActiveDocument == null)
            {
                return null;
            }

            var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            if (null == doc)
            {
                return null;
            }

            var ep = doc.StartPoint.CreateEditPoint();
            ep.EndOfDocument();

            var length = ep.AbsoluteCharOffset;
            ep.StartOfDocument();
            return ep.GetText(length);
        }

14. Example

View license
private string GetCurrentDocumentText()
        {

            var dte = (DTE) GetService(typeof (DTE));

            if (dte.ActiveDocument == null)
            {
                return null;
            }

            var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            if (null == doc)
            {
                return null;
            }

            var ep = doc.StartPoint.CreateEditPoint();
            ep.EndOfDocument();

            var length = ep.AbsoluteCharOffset;
            ep.StartOfDocument();
            return ep.GetText(length);
        }

15. Example

View license
private void ToggleQueryCosts(object sender, EventArgs e)
        {
            try
            {
                var dte = (DTE) GetService(typeof (DTE));

                if (dte.ActiveDocument == null)
                {
                    return;
                }

                var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
                if (null == doc)
                {
                    return;
                }

                var ep = doc.StartPoint.CreateEditPoint();

                ep.EndOfDocument();

                var length = ep.AbsoluteCharOffset;
                ep.StartOfDocument();

                var originalText = ep.GetText(length);

                DocumentScriptCosters.SetDte(dte);

                var coster = DocumentScriptCosters.GetInstance().GetCoster();
                if (coster == null)
                    return;

                if (coster.ShowCosts)
                {
                    coster.ShowCosts = false;
                }
                else
                {
                    coster.ShowCosts = true;
                    coster.AddCosts(originalText, dte.ActiveDocument);
                }
            }
            catch (Exception ee)
            {
                OutputPane.WriteMessage("ToggleQueryCosts error: {0}", ee.Message);
                Log.WriteInfo("ToggleQueryCosts error: {0}", ee.Message);
            }
        }

16. Example

Project: SSDT-DevPack
Source File: DocumentExtensions.cs
View license
public static string GetText(this Document source)
        {
            if (source == null)
                return null;    //can this even happen?

            var doc = source.Object("TextDocument") as TextDocument;

            if (null == doc)
            {
                return null;
            }

            var ep = doc.StartPoint.CreateEditPoint();
            ep.EndOfDocument();

            var length = ep.AbsoluteCharOffset;
            ep.StartOfDocument();
            return ep.GetText(length);
        }

17. Example

Project: MultiversePlatform
Source File: FileCodeModel.cs
View license
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        internal void Reparse() {
            if(isDirty) {
                Debug.Assert(ccu != null);

                CodeCompileUnit newCcu = null;
                TextDocument td = ((TextDocument)ProjectItem.Document.Object("TextDocument"));
                
                Debug.Assert(td != null);
                
                EditPoint ep = td.CreateEditPoint(td.StartPoint);                
                string text = ep.GetText(td.EndPoint);

                try{
                    newCcu = provider.ParseMergable(text,
                        ProjectItem.Document.FullName,
                        new FileCodeMerger(parent));

                    MergeNewCompileUnit(newCcu);
                } catch {
                    // swallow parse errors.
                }                
                isDirty = false;
            }
        }

18. Example

View license
private void CreatetSQLtSchema(object sender, EventArgs e)
        {
            try
            {
                CallWrapper();

                var dte = (DTE) GetService(typeof (DTE));

                if (dte.ActiveDocument == null)
                {
                    return;
                }

                var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
                if (null == doc)
                {
                    return;
                }

                var ep = doc.StartPoint.CreateEditPoint();

                ep.EndOfDocument();

                var length = ep.AbsoluteCharOffset;
                ep.StartOfDocument();

                var originalText = ep.GetText(length);

                var builder = new SchemaBuilder(originalText);
                builder.CreateSchemas();
            }
            catch (Exception ex)
            {
                OutputPane.WriteMessage("Exception creating tSQLt schema, error: {0}", ex.Message);
            }
        }

19. Example

View license
void myDocumentEvents_DocumentSaved(Document Document)
        {
            //kludge
            if (!TinyIoCContainer.Current.CanResolve<IWrapperClassMaker>())
                RegisterTypes();
            if (Document.FullName.EndsWith(".sql"))
                try
                {
                    var textDoc = ((TextDocument)Document.Object());
                    var text = textDoc.CreateEditPoint().GetText(textDoc.EndPoint);
                    if (text.Contains("managed by QueryFirst"))
                    {
                        var cdctr = new Conductor(_VSOutputWindow);
                        cdctr.ProcessOneQuery(Document);
                    }

                }
                catch (Exception ex)
                {
                    _VSOutputWindow.Write(ex.Message + ex.StackTrace);
                }
        }

20. Example

Project: Wax
Source File: DteExtensions.cs
View license
[NotNull]
        public static string GetContent([NotNull] this EnvDTE.ProjectItem projectItem)
        {
            try
            {
                if (!projectItem.IsOpen)
                    projectItem.Open();

                var document = projectItem.Document;

                if (document != null)
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    return GetContent((EnvDTE.TextDocument)document.Object("TextDocument"));
                }

                var fileName = projectItem.TryGetFileName();

                if (string.IsNullOrEmpty(fileName))
                    return string.Empty;

                return File.ReadAllText(fileName);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

21. Example

View license
private void CreatetSQLtTest(object sender, EventArgs e)
        {
            try
            {
                CallWrapper();

                var dte = (DTE) GetService(typeof (DTE));

                if (dte.ActiveDocument == null)
                {
                    return;
                }

                var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
                if (null == doc)
                {
                    return;
                }

                var ep = doc.StartPoint.CreateEditPoint();

                ep.EndOfDocument();

                var length = ep.AbsoluteCharOffset;
                ep.StartOfDocument();

                var originalText = ep.GetText(length);

                var builder = new TestBuilder(originalText, dte.ActiveDocument.ProjectItem.ContainingProject);
                builder.Go();
                //  builder.CreateTests();
            }
            catch (Exception ex)
            {
                OutputPane.WriteMessage("Exception creating tSQLt tests, error: {0}", ex.Message);
            }
        }

22. Example

View license
private void SetCurrentDocumentText(string newText)
        {
            var dte = (DTE)GetService(typeof(DTE));

            if (dte.ActiveDocument == null)
            {
                return;
            }

            var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            if (null == doc)
            {
                return;
            }

            var ep = doc.StartPoint.CreateEditPoint();
            ep.EndOfDocument();
            
            var length = ep.AbsoluteCharOffset;

            ep.StartOfDocument();
            ep.Delete(length);
         
            ep.Insert(newText);
        }

23. Example

Project: query-first
Source File: QueryFirstCmd.cs
View license
private void ProcessAllItems(ProjectItems items, VSOutputWindow vsOutputWindow)
        {
            if (items != null)
            {

                foreach (ProjectItem item in items)
                {
                    try
                    {
                        if (item.FileNames[1].EndsWith(".sql"))
                        {
                            item.Open();
                            var textDoc = ((TextDocument)item.Document.Object());
                            var text = textDoc.CreateEditPoint().GetText(textDoc.EndPoint);
                            if (text.Contains("managed by QueryFirst"))
                            {
                                new Conductor(vsOutputWindow).ProcessOneQuery(item.Document);
                            }
                            
                        }
                        if (item.Kind == "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}") //folder
                            ProcessAllItems(item.ProjectItems, vsOutputWindow);
                    }
                    catch (Exception ex) { }
                }
            }
        }

24. Example

Project: query-first
Source File: PutCodeHere.cs
View license
public void WriteAndFormat(string code)
        {
            bool rememberToClose = false;
            if (!_item.IsOpen)
            {
                _item.Open();
                rememberToClose = true;
            }
            var textDoc = ((TextDocument)_item.Document.Object());
            var ep = textDoc.CreateEditPoint();
            ep.ReplaceText(textDoc.EndPoint, code, 0);
            ep.SmartFormat(textDoc.EndPoint);
            _item.Save();
            if (rememberToClose)
            {
                _item.Document.Close();
            }
        }

25. Example

View license
private void RunCodeCleanupGeneric(Document document, ITextBuffer textBuffer)
        {
            ITextDocument textDocument;

            var doc = (TextDocument)document.Object("TextDocument");
            textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out textDocument);

            var path = doc.Parent.FullName;

            FileConfiguration settings;
            if (!ConfigLoader.TryLoad(path, out settings))
                return;

            using (ITextEdit edit = textBuffer.CreateEdit())
            {
                ITextSnapshot snapshot = edit.Snapshot;

                if (settings.Charset != null && textDocument != null)
                    FixDocumentCharset(textDocument, settings.Charset.Value);

                if (settings.TryKeyAsBool("trim_trailing_whitespace"))
                    TrimTrailingWhitespace(snapshot, edit);

                if (settings.TryKeyAsBool("insert_final_newline"))
                    InsertFinalNewline(snapshot, edit, settings.EndOfLine());

                var eol = settings.EndOfLine();
                FixLineEndings(snapshot, edit, eol);

                edit.Apply();
            }
        }

26. Example

View license
private void NameConstraintsCalled(object sender, EventArgs e)
        {
            try
            {
                CallWrapper();

                var dte = (DTE) GetService(typeof (DTE));
                if (null == dte || dte.ActiveDocument == null)
                {
                    return;
                }

                var doc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
                if (null == doc)
                {
                    return;
                }

                var ep = doc.StartPoint.CreateEditPoint();

                ep.EndOfDocument();

                var length = ep.AbsoluteCharOffset;
                ep.StartOfDocument();

                var originalText = ep.GetText(length);

                var namer = new ConstraintNamer(originalText);
                var modifiedText = namer.Go();

                if (originalText != modifiedText)
                {
                    ep.Delete(length);
                    ep.Insert(modifiedText);
                }
            }
            catch (Exception ex)
            {
                OutputPane.WriteMessage("Exception naming constraints, error: {0}", ex.Message);
            }
        }

27. Example

View license
void CSharpItemRenamed(ProjectItem renamedQuery, string OldName)

        {
            if (OldName./n ..... /n //View Source file for more details /n }

28. Example

View license
public void InjectPOCOFactory(CodeGenerationContext ctx, ProjectItem partialClass)
        {
       /n ..... /n //View Source file for more details /n }