Get the project of the current editing file

148 Views Asked by At

I created my first VS extension following this tutorial.

I would like to the get the project of the current editing file, i.e. related project properties from ITextView textView and ITextBuffer textBuffer.

After some searching, I have found:

  1. get the file path from ITextView

  2. get all project properties

It seems that I could match file path against all projects to decide this, but it is not ideal.

Could I get this information directly somewhere?

1

There are 1 best solutions below

0
Monie On

Hey :) I had the same question last week.

The current open filename:

DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
        string docName = dte.ActiveDocument.Name;

I used ITagger - it's called every time something is changed in your code, so you are always up-to-date. Have a look at this tutorial.

IEnumerable<ITagSpan<IssueTag>> ITagger<IssueTag>.GetTags(NormalizedSnapshotSpanCollection spans)
    {
        DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
        string docName = dte.ActiveDocument.Name;

        foreach (SnapshotSpan span in spans)
        {
            //look at each classification span \
            foreach (ClassificationSpan classification in m_classifier.GetClassificationSpans(span))
            {
                //if the classification is a comment
                if (classification.ClassificationType.Classification.ToLower().Contains("comment"))
                {
                    //if the word "todo" is in the comment,
                    //create a new TodoTag TagSpan
                    int index = classification.Span.GetText().ToLower().IndexOf(m_searchText);
                    if (index != -1)
                    {
                        yield return new TagSpan<IssueTag>(new SnapshotSpan(classification.Span.Start + index, m_searchText.Length), new IssueTag());
                    }
                }
            }
        }