This chapter would not quite fit into the printed SharpDevelop book so it has been decided that it will be released online.
作者认为不太适合印刷,所以发布网上了。
其实这是介绍sharpdevelop怎么开发别的语言版本而写的。
下载地址:
http://files.cnblogs.com/cyangluiqin/8171_Chap18.pdf
Someone recently asked me by mail about adding IronPython support. This should help everyone until we have "real" documentation for writing backend bindings.
The first step is creating a project type and the compiler binding. For the compiler, you need to write a MSBuild task for the compiler. Then you'll need a targets file that can be included by MSBuild and calls that task.
In the SharpDevelop source code, take a look at SharpDevelop\src\Libraries\ICSharpCode.Build.Tasks\Project\ILAsm.cs and SharpDevelop.Build.MSIL.targets
However it is possible that the Visual Studio IronPython integration already comes with the MSBuild task and targets file; then you can just use that.
Now the real start: Create a new SharpDevelop AddIn project.
The video at http://laputa.sharpdevelop.net/WritingASharpDevelopAddInTutorialVideo.aspx should help you understand the basics. Don't forget to look at the SharpDevelop\doc\technotes directory (in the SharpDevelop source code download).
Then you should look at the ILAsmBinding source code (it's the simplest backend binding possible). When you look at ILAsmBinding.addin, you'll see the most important things to provide:
1. FileFilter entry. Just copy it from ILAsmBinding and adjust to your needs.
2. Templates: The XML entry tells SharpDevelop to look for a "Templates" directory inside the directory containing your AddIn assembly. Just create such a directory in the project browser and add a .xpt file to it, then set "Copy to output directory" for the .xpt file to "Always".
For writing the .xpt project template, just use the one from ILAsmBinding and adjust it to your needs. The important part is the <LanguageName> property: this must match the language name used in the next step.
3. Language Binding. Use the <LanguageBinding> codon to register your language binding with SharpDevelop. Try to use the file extension and GUID from the Visual Studio IronPython AddIn; or create your own GUID (Tools > Insert New GUID) and use a new file extension.
For writing the LanguageBinding class: just adapt the ILAsmLanguageBinding to your needs. The "single file compilation" is not supported by SharpDevelop 2.0, it'll always use MSBuild. The important methods left are LoadProject and CreateProject. Both have to return a new instance of your project class. The project class can be really simple (again, look at ILAsmProject) if you inherit from MSBuildProject. Just set the language name; don't forget to call base.Create(info) in the new project constructor / SetupProject(fileName) in the load project constructor. In the constructor creating a new project, you have to use "imports.Add()" to add a reference to your MSBuild target file. If you put your targets file in your AddIn's directory, you can create your own MSBuild property like this:
static bool initialized = false; static void Init() { if (!initialized) { initialized = true; MSBuildEngine.CompileTaskNames.Add("nameOfMyMSBuildCompilerTask"); // makes SharpDevelop show the "Compiling ProjectName..."-line when compiling your projects MSBuildEngine.MSBuildProperties.Add("MyLanguageBinPath", Path.GetDirectoryName(typeof(MyProject).Assembly.Location)); } }
Make sure you call Init() in both constructors of your project class.
4. ILAsmBinding.addin also contains the project options section. The dialog panels reference panels in the main SharpDevelop code that can be used for all (.NET) languages. If you use them, you need a "<Import assembly = ":ICSharpCode.SharpDevelop"/>" entry in your <Runtime> section.
5. Syntax mode for the text editor should be compiled as embedded resource and can be loaded using the <SyntaxMode> codon. You don't need this at the beginning.
This is everything you need for very simple integration of a language.
The next step after this is providing a parser for the language. That is a bit complicated (especially if you cannot use the parser from the IronPython compiler and have to write your own), but it will enable folding and the class browser (both the "Classes" pad and the combo boxes above the code editor).
第十八章部份笔记
src\BackendBindings\CSharpLanguageModule\FormattingStrategy\CSharpFormattingStrategy.cs 即cs的格式化策略,意在让不同的语言有不同的缩进方式
自动缩进的代码:
/// <summary> /// Define CSharp specific smart indenting for a line :) /// </summary> protected override int SmartIndentLine(TextArea textArea, int lineNr) { if (lineNr <= 0) { return AutoIndentLine(textArea, lineNr); } string oldText = textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr)); DocumentAccessor acc = new DocumentAccessor(textArea.Document, lineNr, lineNr); IndentationSettings set = new IndentationSettings(); set.IndentString = Tab.GetIndentationString(textArea.Document); set.LeaveEmptyLines = false; IndentationReformatter r = new IndentationReformatter(); r.Reformat(acc, set); string t = acc.Text; if (t.Length == 0) { // use AutoIndentation for new lines in comments / verbatim strings. return AutoIndentLine(textArea, lineNr); } else { int newIndentLength = t.Length - t.TrimStart().Length; int oldIndentLength = oldText.Length - oldText.TrimStart().Length; if (oldIndentLength != newIndentLength && lineNr == textArea.Caret.Position.Y) { // fix cursor position if indentation was changed int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength; textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), lineNr); } return newIndentLength; } }
这种缩进,到底有多少是JAVA需要的呢?
SharpDevelop的三个选项对话框来配置项目和编译器选项:
输出生成选项
代码编生成选项
编辑器/运行时选项