Ejemplo n.º 1
0
        public CodeModel CreatePreprocessedModel(ProbeAppSettings appSettings, string fileName, VsText.ITextSnapshot snapshot, bool visible, string reason)
        {
            CodeSource source;
            IEnumerable <Preprocessor.IncludeDependency> includeDependencies = null;

            if (visible)
            {
                source = new CodeSource();
                source.Append(snapshot.GetText(), fileName, 0, snapshot.Length, true, true, false);
                source.Flush();
            }
            else
            {
                var merger = new FileMerger();
                merger.MergeFile(appSettings, fileName, snapshot.GetText(), false, true);
                source = merger.MergedContent;

                includeDependencies = (from f in merger.FileNames
                                       select new Preprocessor.IncludeDependency(f, false, true, merger.GetFileContent(f))).ToArray();
            }

            var model = CreatePreprocessedModel(appSettings, source, fileName, visible, reason, includeDependencies);

            model.Snapshot = snapshot;
            return(model);
        }
Ejemplo n.º 2
0
            private static CodeModel.CodeSource GetCodeSourceForActiveView(out string fileName)
            {
                ThreadHelper.ThrowIfNotOnUIThread();

                var view = Shell.ActiveView;

                if (view != null)
                {
                    fileName = VsTextUtil.TryGetDocumentFileName(view.TextBuffer);
                    var content = view.TextBuffer.CurrentSnapshot.GetText();

                    try
                    {
                        var merger = new CodeModel.FileMerger();
                        merger.MergeFile(ProbeEnvironment.CurrentAppSettings, fileName, content, true, true);
                        return(merger.MergedContent);
                    }
                    catch (Exception ex)
                    {
                        Log.WriteEx(ex);

                        var codeSource = new CodeModel.CodeSource();
                        codeSource.Append(content, new CodeModel.CodeAttributes(fileName, 0, content.Length, true, true, false));
                        codeSource.Flush();

                        return(codeSource);
                    }
                }

                fileName = null;
                return(null);
            }
Ejemplo n.º 3
0
        public CodeModel CreatePreprocessedModel(ProbeAppSettings appSettings, string fileName, VsText.ITextSnapshot snapshot, string reason)
        {
            var source = new CodeSource();

            source.Append(snapshot.GetText(), fileName, 0, snapshot.Length, true, true, false);
            source.Flush();

            var model = CreatePreprocessedModel(appSettings, source, fileName, true, reason, null);

            model.Snapshot = snapshot;
            return(model);
        }
Ejemplo n.º 4
0
        public void MergeFile(ProbeAppSettings appSettings, string fileName, string content, bool showMergeComments, bool fileIsPrimary)
        {
            if (appSettings == null)
            {
                throw new ArgumentNullException(nameof(appSettings));
            }

            _appSettings = appSettings;

            // Locate all needed copies of files
            _primaryFileName = fileName;
            _origFileName    = "";
            _localFileNames.Clear();
            _showMergeComments = showMergeComments;

            var rawFileName = fileName;

            if (Path.IsPathRooted(fileName))
            {
                fileName = UnrootFileName(fileName);
            }
            FindFiles(fileName);

            if (string.IsNullOrEmpty(_origFileName))
            {
                throw new FileMergeException(string.Format("Could not find base file for '{0}'.", rawFileName));
            }

            if (content == null)
            {
                _origContent = File.ReadAllText(_origFileName);
            }
            else
            {
                _origContent = content;
            }

            // Perform localization
            CreateLineDataFromOrigFile(_origContent);
            foreach (string localFileName in _localFileNames)
            {
                try
                {
                    MergeFile(localFileName);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Error when merging local file '{0}' into '{1}'.", localFileName, fileName);
                }
            }

            // Generate the final source.
            _mergedContent = new CodeSource();
            int lineIndex = 0;

            foreach (var line in _lines)
            {
                var primary = fileIsPrimary && _primaryFileName.Equals(line.fileName, StringComparison.OrdinalIgnoreCase);
                var endPos  = line.pos + line.text.Length;
                _mergedContent.Append(line.text, new CodeAttributes(line.fileName, line.pos, endPos, true, primary, false));
                if (!line.text.EndsWith("\n") && lineIndex + 1 < _lines.Count)
                {
                    // Does not end with crlf. Need to insert between the lines.
                    _mergedContent.Append("\r\n", new CodeAttributes(line.fileName, endPos, endPos, false, primary, false));
                }
                lineIndex++;
            }
            _mergedContent.Flush();
        }