Beispiel #1
0
        private void HandleInclude(
            [CanBeNull] string includeFileName,
            [NotNull] T4DirectiveAttribute fileAttr,
            [NotNull] CompositeElement parentElement,
            bool once
            )
        {
            FileSystemPath includePath = ResolveInclude(includeFileName);

            if (includePath.IsEmpty)
            {
                fileAttr.ValueError = String.Format(CultureInfo.InvariantCulture, "Unresolved file \"{0}\"", includePath);
                return;
            }

            if (!_existingIncludePaths.Add(includePath))
            {
                if (!once)
                {
                    fileAttr.ValueError = String.Format(CultureInfo.InvariantCulture, "Already included file \"{0}\"", includePath);
                }
                return;
            }

            FileSystemPath sourceLocation = _sourceFile.GetLocation();

            if (includePath == sourceLocation)
            {
                fileAttr.ValueError = "Recursive include";
                _existingIncludePaths.Add(includePath);
                return;
            }

            if (!includePath.ExistsFile)
            {
                fileAttr.ValueError = String.Format(CultureInfo.InvariantCulture, "File \"{0}\" not found", includePath);
                return;
            }

            // find the matching include in the existing solution source files
            // or create a new one if the include file is outside the solution
            IPsiSourceFile includeSourceFile = includePath.FindSourceFileInSolution(_solution) ?? CreateIncludeSourceFile(includePath);

            if (includeSourceFile == null)
            {
                fileAttr.ValueError = "No current solution";
                return;
            }

            ILexer    includeLexer = CreateLexer(includeSourceFile);
            var       builder      = new T4TreeBuilder(_t4Environment, _directiveInfoManager, includeLexer, includeSourceFile, _existingIncludePaths, _solution, _macroResolveModule);
            T4Include include      = builder.CreateIncludeT4Tree();

            include.Path = includePath;
            _includes.Add(include);

            // do not use AppendNewChild, we don't want the PsiBuilderLexer to move line breaks from the include into the main file.
            parentElement.AddChild(include);
        }
                internal T4DirectiveAttribute ToAttribute()
                {
                    var attribute = new T4DirectiveAttribute();

                    if (NameToken != null)
                    {
                        _builder.AppendNewChild(attribute, NameToken);
                        if (EqualToken != null)
                        {
                            _builder.AppendNewChild(attribute, EqualToken);
                        }
                        else
                        {
                            if (OpeningQuoteToken == null)
                            {
                                Assertion.Assert(ValueToken == null, "ValueToken should be null if there's no opening quote.");
                                Assertion.Assert(ClosingQuoteToken == null, "ClosingQuoteToken should be null if there's no opening quote.");
                                _builder.AppendMissingToken(attribute, MissingTokenType.EqualSignAndAttributeValue);
                                return(attribute);
                            }
                            _builder.AppendMissingToken(attribute, MissingTokenType.EqualSign);
                        }
                    }
                    else if (EqualToken != null)
                    {
                        _builder.AppendMissingToken(attribute, MissingTokenType.AttributeName);
                        _builder.AppendNewChild(attribute, EqualToken);
                    }
                    else
                    {
                        _builder.AppendMissingToken(attribute, MissingTokenType.AttributeNameAndEqualSign);
                    }

                    if (OpeningQuoteToken == null)
                    {
                        Assertion.Assert(ValueToken == null, "ValueToken should be null if there's no opening quote.");
                        Assertion.Assert(ClosingQuoteToken == null, "ClosingQuoteToken should be null if there's no opening quote.");
                        _builder.AppendMissingToken(attribute, MissingTokenType.AttributeValue);
                        return(attribute);
                    }

                    _builder.AppendNewChild(attribute, OpeningQuoteToken);
                    if (ValueToken != null)
                    {
                        _builder.AppendNewChild(attribute, ValueToken);
                    }
                    if (ClosingQuoteToken != null)
                    {
                        _builder.AppendNewChild(attribute, ClosingQuoteToken);
                    }
                    else
                    {
                        _builder.AppendMissingToken(attribute, MissingTokenType.Quote);
                    }

                    return(attribute);
                }