5
// Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com>
7
// Copyright (c) 2010 Levi Bard
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Text.RegularExpressions;
31
using MonoDevelop.Core;
32
using MonoDevelop.Projects.Dom;
33
using MonoDevelop.Projects.Dom.Parser;
35
namespace MonoDevelop.VersionControl.Views
38
/// Parser for unified diffs
40
public class DiffParser: AbstractParser
42
// Match the original file and time/revstamp line, capturing the filepath and the stamp
43
static Regex fileHeaderExpression = new Regex (@"^---\s+(?<filepath>[^\t]+)\t(?<stamp>.*)$", RegexOptions.Compiled);
45
// Match a chunk header line
46
static Regex chunkExpression = new Regex (@"^@@\s+(?<chunk>-\d+,\d+\s+\+\d+,\d+)\s+@@", RegexOptions.Compiled);
48
// Capture the file's EOL string
49
static Regex eolExpression = new Regex (@"(?<eol>\r\n|\n|\r)", RegexOptions.Compiled);
51
public DiffParser (): base("Diff", "text/x-diff")
55
#region AbstractParser overrides
57
public override bool CanParse (string fileName)
59
if (!string.IsNullOrEmpty (fileName)) {
60
string extension = Path.GetExtension (fileName);
61
return (".diff".Equals (extension, StringComparison.OrdinalIgnoreCase) ||
62
".patch".Equals (extension, StringComparison.OrdinalIgnoreCase));
68
public override bool CanParseMimeType (string mimeType)
70
return (!string.IsNullOrEmpty (mimeType) &&
71
"text/x-diff".Equals (mimeType.Trim (), StringComparison.Ordinal));
74
public override bool CanParseProjectType (string projectType)
79
public override ParsedDocument Parse (ProjectDom dom, string fileName, string content)
81
ParsedDocument doc = new ParsedDocument (fileName);
82
if(null == doc.CompilationUnit)
83
doc.CompilationUnit = new CompilationUnit (fileName);
84
CompilationUnit cu = (CompilationUnit)doc.CompilationUnit;
85
DomType currentFile = null;
86
DomProperty currentRegion = null;
88
string eol = Environment.NewLine;
89
Match eolMatch = eolExpression.Match (content);
90
if (eolMatch != null && eolMatch.Success)
91
eol = eolMatch.Groups["eol"].Value;
93
string[] lines = content.Split (new string[]{eol}, StringSplitOptions.None);
96
foreach (string line in lines)
98
lineMatch = fileHeaderExpression.Match (line.Trim());
99
if (lineMatch != null && lineMatch.Success) {
100
if (currentFile != null) // Close out previous file region
101
currentFile.BodyRegion = new DomRegion (currentFile.BodyRegion.Start.Line,
102
currentFile.BodyRegion.Start.Column,
103
linenum-1, int.MaxValue);
104
if (currentRegion != null) // Close out previous chunk region
105
currentRegion.BodyRegion = new DomRegion (currentRegion.BodyRegion.Start.Line,
106
currentRegion.BodyRegion.Start.Column,
107
linenum-1, int.MaxValue);
109
// Create new file region
110
currentFile = new DomType (cu, ClassType.Unknown, Modifiers.None,
111
lastToken (lineMatch.Groups["filepath"].Value),
112
new DomLocation (linenum, 1),
114
new DomRegion (linenum, line.Length+1, linenum, int.MaxValue));
115
cu.Add (currentFile);
117
lineMatch = chunkExpression.Match (line);
118
if (lineMatch != null && lineMatch.Success) {
119
if (currentRegion != null) // Close out previous chunk region
120
currentRegion.BodyRegion = new DomRegion (currentRegion.BodyRegion.Start.Line,
121
currentRegion.BodyRegion.Start.Column,
122
linenum-1, int.MaxValue);
124
// Create new chunk region
125
currentRegion = new DomProperty (lineMatch.Groups["chunk"].Value, Modifiers.None,
126
new DomLocation (linenum, 1),
127
new DomRegion (linenum, line.Length+1, linenum, int.MaxValue), null);
128
currentFile.Add (currentRegion);
134
// Close out trailing regions
135
if (currentFile != null)
136
currentFile.BodyRegion = new DomRegion (currentFile.BodyRegion.Start.Line,
137
currentFile.BodyRegion.Start.Column,
138
Math.Max (1, linenum-2), int.MaxValue);
139
if (currentRegion != null)
140
currentRegion.BodyRegion = new DomRegion (currentRegion.BodyRegion.Start.Line,
141
currentRegion.BodyRegion.Start.Column,
142
Math.Max (1, linenum-2), int.MaxValue);
149
// Return the last token from a filepath
150
// (delimiter may not match Path.DirectorySeparatorChar)
151
static string lastToken (string filepath)
153
if (!string.IsNullOrEmpty (filepath)) {
154
string[] tokens = filepath.Split (new char[]{'\\','/'}, StringSplitOptions.RemoveEmptyEntries);
155
return tokens[tokens.Length-1];