/// <summary> /// Extracts a snippet from a given rule pattern. /// </summary> /// <param name="fullFilename">The full filename (with path) to load and to extract the snippet from.</param> /// <param name="memberPattern">The member pattern to extract.</param> /// <returns>The extracted snippet.</returns> public override string Extract(string fullFilename, string memberPattern) { // Return the entire code if no member is specified if (string.IsNullOrWhiteSpace(memberPattern)) { return base.Extract(fullFilename, memberPattern); } // Parse the matching rule from the pattern CSharpMatchingRule rule = CSharpMatchingRule.Parse(memberPattern); // Load the trie for pattern matching if (null == this.syntaxTrie) { // Load file content string sourceCode = this.LoadFile(fullFilename); // Build a syntax tree from the source code SyntaxTree tree = CSharpSyntaxTree.ParseText(sourceCode); SyntaxNode root = tree.GetRoot(); // Visit the syntax tree for generating a Trie for pattern matching CSharpSyntaxWalkerMatchingBuilder syntaxMatchingBuilder = new CSharpSyntaxWalkerMatchingBuilder(); syntaxMatchingBuilder.Visit(root); // Retrieve the Trie root this.syntaxTrie = syntaxMatchingBuilder.Root; } // Match the rule from the syntax matching Trie CSharpSyntaxMatchingNode matchingTrie = syntaxTrie.Match(rule.MatchingChunks); if (null == matchingTrie) { throw new SnippetExtractionException("Cannot find member", memberPattern); } // Build a snippet for extracted syntax nodes return this.BuildSnippet(matchingTrie.MatchingSyntaxNodes, rule.ExtractionMode); }
/// <summary> /// Extracts a snippet from a given rule pattern. /// </summary> /// <param name="rule">The rule to parse and extract snippet from.</param> /// <returns>The extracted snippet.</returns> public Model.Snippet Extract() { // Parse the matching rule from the pattern CSharpMatchingRule rule = CSharpMatchingRule.Parse(this.Pattern); // Look for the file in available source directories FileInfo fileInfo = null; foreach (DirectoryInfo directoryInfo in this.SourceDictionaries) { string filePath = Path.Combine(directoryInfo.FullName, rule.TargetFile); if (File.Exists(filePath)) { fileInfo = new FileInfo(filePath); break; } } // Raise an error if cannot find the file if (null == fileInfo) { throw new SnippetExtractionException("Cannot find file in any referenced project", rule.TargetFile); } // Load the file content MemoryStream memoryStream = new MemoryStream(); using (var fileReader = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open))) using (var fileWriter = new StreamWriter(memoryStream)) { fileWriter.Write(fileReader.ReadToEnd()); } // Read the code snippet from the file string sourceCode = Encoding.UTF8.GetString(memoryStream.ToArray()); // Return the entire code if no member is specified if (rule.MatchingChunks.Length <= 0) { return this.BuildSnippet(sourceCode); } // Build a syntax tree from the source code SyntaxTree tree = CSharpSyntaxTree.ParseText(sourceCode); SyntaxNode root = tree.GetRoot(); // Visit the syntax tree for generating a Trie for pattern matching CSharpSyntaxWalkerMatchingBuilder syntaxMatchingBuilder = new CSharpSyntaxWalkerMatchingBuilder(); syntaxMatchingBuilder.Visit(root); // Match the rule from the syntax matching Trie CSharpSyntaxMatchingNode node = syntaxMatchingBuilder.Root.Match(rule.MatchingChunks); if (null == node) { throw new SnippetExtractionException("Cannot find member", this.Pattern); } // Build a snippet for extracted syntax nodes return this.BuildSnippet(node.MatchingSyntaxNodes, rule.ExtractionMode); }