Ejemplo n.º 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);
        }