pinvokecodegenerator.cs

来自「c#源代码」· CS 代码 · 共 102 行

CS
102
字号
//
// ${App.Name}
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Actions;
using ICSharpCode.TextEditor.Document;
using System;

namespace ICSharpCode.PInvokeAddIn
{
	/// <summary>
	/// Generates PInvoke signature code in the SharpDevelop text editor.
	/// </summary>
	public class PInvokeCodeGenerator
	{
		int numOperations;
		
		public PInvokeCodeGenerator()
		{
		}
		
		/// <summary>
		/// Inserts the PInvoke signature at the current cursor position.
		/// </summary>
		/// <param name="textArea">The text editor.</param>
		/// <param name="signature">A PInvoke signature string.</param>
		public void Generate(TextArea textArea, string signature)
		{
			numOperations = 0;
			IndentStyle oldIndentStyle = textArea.TextEditorProperties.IndentStyle;
			bool oldEnableEndConstructs = SharpDevelopApplication.PropertyService.GetProperty("VBBinding.TextEditor.EnableEndConstructs", true);

			try {

				textArea.BeginUpdate();
				textArea.TextEditorProperties.IndentStyle = IndentStyle.Smart;
				SharpDevelopApplication.PropertyService.SetProperty("VBBinding.TextEditor.EnableEndConstructs", false);

				string[] lines = signature.Replace("\r\n", "\n").Split('\n');
				
				for (int i = 0; i < lines.Length; ++i) {
					
					textArea.InsertString(lines[i]);
					++numOperations;
					
					// Insert new line if not the last line.
					if ( i < (lines.Length - 1))
					{
						Return(textArea);
					}
				}
				
				if (numOperations > 0) {
					textArea.Document.UndoStack.UndoLast(numOperations);
				}
				
			} finally {
				textArea.TextEditorProperties.IndentStyle = oldIndentStyle;
				SharpDevelopApplication.PropertyService.SetProperty("VBBinding.TextEditor.EnableEndConstructs", oldEnableEndConstructs);
				textArea.EndUpdate();
				textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
				textArea.Document.CommitUpdate();	
			}
		}
		
		void Return(TextArea textArea)
		{
			IndentLine(textArea);
			new Return().Execute(textArea);
			++numOperations;
		}
		
		void IndentLine(TextArea textArea)
		{
			int delta = textArea.Document.FormattingStrategy.IndentLine(textArea, textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset));
			if (delta != 0) {
				++numOperations;
				LineSegment caretLine = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
				textArea.Caret.Position = textArea.Document.OffsetToPosition(Math.Min(textArea.Caret.Offset + delta, caretLine.Offset + caretLine.Length));
			}
		}		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?