title: Get Roslyn SyntaxToken from Visual Studio Text Selection (caret position)
body: <p>I am attempting to bridge between the VSSDK and Roslyn SDK in a Visual Studio extension package and have been having a hard time with this.
  The ActivePoint.AbsoluteCharOffset given from Visual Studio does not match the element I get from Roslyn when using FindToken(offset). I'm fairly sure this has to do with how each side counts EOL characters based on my current working hack but I'm not 100% that my hack is going to hold up in the long run.</p>
  
  <p>My hack is this line: <code>charOffset += point.Line;</code></p>
  
  <p>I add the number of lines onto the char offset, this seems to work so I'm guessing I am adding in all the line break characters that are being ignored by activepoint counting.</p>
  
  <p>Helpers</p>
  
  <pre><code>private VisualStudioWorkspace workspace = null;
  public RoslynUtilities(VisualStudioWorkspace workspace)
  {
      this.workspace = workspace;
  }
  public Solution Solution { get { return workspace.CurrentSolution; } }
  public Document GetDocumentFromPath(string fullPath)
  {
      foreach (Project proj in this.Solution.Projects)
      {               
          foreach (Document doc in proj.Documents)
          {
              if (doc.FilePath == fullPath)
                  return doc;                   
          }
      }
      return null;
  }
  public SyntaxTree GetSyntaxTreeFromDocumentPath(string fullPath)
  {
      Document doc = GetDocumentFromPath(fullPath);
      if (doc != null)
          return doc.GetSyntaxTreeAsync().Result;
      else
          return null;
  }
  public SyntaxNode GetNodeByFilePosition(string fullPath, int absoluteChar)
  {
      SyntaxTree tree = GetSyntaxTreeFromDocumentPath(fullPath);
      if(tree != null)
      {
          var compUnit = tree.GetCompilationUnitRoot();
          if(compUnit != null)
          {
              return compUnit.FindToken(absoluteChar, true).Parent;
          }
      }
      return null;                        
  }
  private VisualStudioWorkspace GetRoslynWorkspace()
      {
          var componentModel = (IComponentModel)GetGlobalService(typeof(SComponentModel));
          return componentModel.GetService&lt;VisualStudioWorkspace&gt;();
      }
  </code></pre>
  
  <p>Main Part</p>
  
  <pre><code>EnvDTE80.DTE2 applicationObject = (EnvDTE80.DTE2)GetService(typeof(SDTE));
  EnvDTE.TextSelection ts = applicationObject.ActiveWindow.Selection as EnvDTE.TextSelection;
  if (ts == null)
      return;
  
  EnvDTE.VirtualPoint point = ts.ActivePoint;
  int charOffset = point.AbsoluteCharOffset;
  charOffset += point.Line;//HACK ALERT
  
  Parse.Roslyn.RoslynUtilities roslyn = new Parse.Roslyn.RoslynUtilities(GetRoslynWorkspace());
  SyntaxNode node = roslyn.GetNodeByFilePosition(applicationObject.ActiveDocument.FullName, charOffset);
  </code></pre>