protected override TreeTextRange GetCodeTreeTextRange()

in Backend/Core/ForTea.Core/Psi/T4CSharpCustomModificationHandler.cs [70:111]


    protected override TreeTextRange GetCodeTreeTextRange(IT4CodeBlock codeBlock) =>
      codeBlock.Code?.GetTreeTextRange() ?? TreeTextRange.InvalidRange;

    /// <summary>Creates a T4 import directive instead of a C# using directive.</summary>
    /// <param name="before"><c>true</c> to create the directive before <paramref name="anchor"/>; <c>false</c> to create it after.</param>
    /// <param name="anchor">An existing directive serving as an anchor for the new directive.</param>
    /// <param name="usingDirective">The C# using directive.</param>
    /// <param name="originalFile">The original T4 file where the directive must be created.</param>
    /// <returns>A <see cref="TreeTextRange"/> corresponding to the namespace in the newly created directive.</returns>
    protected override bool CreateAndMapUsingNode(bool before, ITreeNode anchorNode, ITreeNode usingDirective,
      IFile originalFile)
    {
      var anchor = anchorNode as IT4Directive;
      if (anchorNode != null && anchor == null) return false;
      
      var t4File = (IT4File)originalFile;
      string ns = GetNamespaceFromUsingDirective(usingDirective);
      var directive = T4DirectiveInfoManager.Import.CreateDirective(ns);

      if (anchor != null && anchor.GetContainingNode<IT4IncludeDirective>() == null)
        directive = before
          ? t4File.AddDirectiveBefore(directive, anchor)
          : t4File.AddDirectiveAfter(directive, anchor);
      else
        directive = t4File.AddDirective(directive);

      var csharpFile = usingDirective.GetContainingFile();
      if (csharpFile == null) return true;
      var csharpUsingRange = GetNameRange(usingDirective);
      if (!csharpUsingRange.IsValid()) return false;

      var t4AttributeValueRange = directive
        .GetAttributeValueToken(T4DirectiveInfoManager.Import.NamespaceAttribute.Name).GetTreeTextRange();
      if (!t4AttributeValueRange.IsValid()) return false;

      csharpFile.GetRangeTranslator().AddProjectionItem(
        new TreeTextRange<Generated>(csharpUsingRange),
        new TreeTextRange<Original>(t4AttributeValueRange)
      );

      return true;
    }