Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DocFoodTextEditorExtension.cs « MonoDevelop.DocFood « MonoDevelop.DocFood « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19529874b427f0c779749da0ef925cbb34bf641e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 
// TextEditorExtension.cs
//  =
// Author:
//       Mike Krüger <mkrueger@novell.com>
// 
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using MonoDevelop.Ide.Editor;
using MonoDevelop.Ide.Editor.Extension;

namespace MonoDevelop.DocFood
{
	class DocFoodTextEditorExtension : TextEditorExtension
	{
		string GenerateDocumentation (ISymbol member, string indent)
		{
			string doc = DocumentBufferHandler.GenerateDocumentation (Editor, member, indent);
			int trimStart = (Math.Min (doc.Length - 1, indent.Length + "//".Length));
			return doc.Substring (trimStart).TrimEnd ('\n', '\r');
		}
		
		string GenerateEmptyDocumentation (ISymbol member, string indent)
		{
			string doc = DocumentBufferHandler.GenerateEmptyDocumentation (Editor, member, indent);
			int trimStart = (Math.Min (doc.Length - 1, indent.Length + "//".Length));
			return doc.Substring (trimStart).TrimEnd ('\n', '\r');
		}

		public override bool KeyPress (KeyDescriptor descriptor)
		{
			if (descriptor.KeyChar != '/')
				return base.KeyPress (descriptor);
			
			var line = Editor.GetLine (Editor.CaretLine);
			string text = Editor.GetTextAt (line.Offset, line.Length);
			
			if (!text.EndsWith ("//", StringComparison.Ordinal))
				return base.KeyPress (descriptor);

			// check if there is doc comment above or below.
			var l = line.PreviousLine;
			while (l != null && l.Length == 0)
				l = l.PreviousLine;
			if (l != null && Editor.GetTextAt (l).TrimStart ().StartsWith ("///", StringComparison.Ordinal))
				return base.KeyPress (descriptor);

			l = line.NextLine;
			while (l != null && l.Length == 0)
				l = l.NextLine;
			if (l != null && Editor.GetTextAt (l).TrimStart ().StartsWith ("///", StringComparison.Ordinal))
				return base.KeyPress (descriptor);

			var member = GetMemberToDocument ();
			if (member == null)
				return base.KeyPress (descriptor);
			
			string documentation = GenerateDocumentation (member, Editor.GetLineIndent (line));
			if (string.IsNullOrEmpty (documentation))
				return base.KeyPress (descriptor);
			
			string documentationEmpty = GenerateEmptyDocumentation (member, Editor.GetLineIndent (line));
			
			int offset = Editor.CaretOffset;
			
			int insertedLength;
			
			// Insert key (3rd undo step)
			Editor.InsertText (offset, "/");
			using (var undo = Editor.OpenUndoGroup ()) {
				documentationEmpty = Editor.FormatString (offset, documentationEmpty); 
				insertedLength = documentationEmpty.Length;
				Editor.ReplaceText (offset, 1, documentationEmpty);
				// important to set the caret position here for the undo step
				Editor.CaretOffset = offset + insertedLength;
			}
			
			using (var undo = Editor.OpenUndoGroup ()) {
				documentation = Editor.FormatString (offset, documentation); 
				Editor.ReplaceText (offset, insertedLength, documentation);
				insertedLength = documentation.Length;
				if (SelectSummary (offset, insertedLength, documentation) == false)
					Editor.CaretOffset = offset + insertedLength;
			}
			return false;
		}

		/// <summary>
		/// Make the summary content selected
		/// </summary>
		/// <returns>
		/// <c>true</c>, if summary was selected, <c>false</c> if summary was not found.
		/// </returns>
		/// <param name='offset'>
		/// Offset in document where the documentation is inserted
		/// </param>
		/// <param name='insertedLength'>
		/// the length of the summary content.
		/// </param>
		/// <param name='documentation'>
		/// Documentation containing the summary
		/// </param>
		bool SelectSummary (int offset, int insertedLength, string documentation)
		{
			//Adjust the line endings to what the document uses to assure correct offset within the documentation
			if (insertedLength > documentation.Length)
				documentation = documentation.Replace ("\n", "\r\n");

			const string summaryStart = "<summary>";
			const string summaryEnd = "</summary>";
			int start = documentation.IndexOf (summaryStart, StringComparison.Ordinal);
			int end = documentation.IndexOf (summaryEnd, StringComparison.Ordinal);
			if (start < 0 || end < 0)
				return false;
			start += summaryStart.Length;
			string summaryText = documentation.Substring (start, end - start).Trim (new char[] {' ', '\t', '\r', '\n', '/'});
			start = documentation.IndexOf (summaryText, start, StringComparison.Ordinal);
			if (start < 0)
				return false;
			Editor.CaretOffset = offset + start;
			Editor.SetSelection (offset + start, offset + start + summaryText.Length);
			return true;
		}

		bool IsEmptyBetweenLines (int start, int end)
		{
			for (int i = start + 1; i < end - 1; i++) {
				var lineSegment = Editor.GetLine (i);
				if (lineSegment == null)
					break;
				if (lineSegment.Length != Editor.GetLineIndent (lineSegment).Length)
					return false;
				
			}
			return true;
		}	
		
		ISymbol GetMemberToDocument ()
		{
			var parsedDocument = DocumentContext.ParsedDocument;
			if (parsedDocument == null)
				return null;
			var semanticModel = parsedDocument.GetAst<SemanticModel> ();
			if (semanticModel == null)
				return null;
			var caretOffset = Editor.CaretOffset;
			var offset = caretOffset;
			var root = semanticModel.SyntaxTree.GetRoot ();

			while (offset < Editor.Length) {
				var node = root.FindNode (TextSpan.FromBounds (offset, offset));
				if (node == null || node.SpanStart < caretOffset) {
					offset++;
					continue;
				}

				var declaredSymbol = semanticModel.GetDeclaredSymbol (node); 
				if (declaredSymbol != null)
					return declaredSymbol;
				offset = node.FullSpan.End + 1;
			}
			return null;
		}
	}
}