public override void ModifyTags(SolutionItem policyParent, Project project, string language, string identifier, string fileName, ref Dictionary <string, string> tags) { tags ["Doctype"] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; //get a language binding IDotNetLanguageBinding langbinding = null; if (project == null) { langbinding = (IDotNetLanguageBinding)LanguageBindingService.GetBindingPerLanguageName(language); } else { DotNetProject dnp = (DotNetProject)project; langbinding = dnp.LanguageBinding; } // work out the ASP.NET language code. Although it's originally a hack around MD's VBNet language name // not corresponding to any of the valid ASP.NET codes, we also provide an interface that // non-core language bindings can implement to advertise that they support ASP.NET string languageCode = language; if (langbinding is IAspNetLanguageBinding) { languageCode = ((IAspNetLanguageBinding)langbinding).AspNetLanguageCode; } else if (language == "VBNet") { languageCode = "VB"; } else if (language != "C#") { LoggingService.LogWarning("The language binding '{0}' does not have explicit support for ASP.NET", language); } tags ["AspNetLanguage"] = languageCode; base.ModifyTags(policyParent, project, language, identifier, fileName, ref tags); //nothing after this point is relevant to tag substitution for filenames, //and some will even crash, so drop out now if (fileName == null) { return; } // Build tags for ${CodeRegion:#} substitutions foreach (string regionName in codeAreas.Keys) { var templ = (CodeTranslationFileDescriptionTemplate)codeAreas [regionName]; //makes CodeTranslationFile's internal name substitition easier var name = (string)tags ["Name"]; templ.GetFileName(policyParent, project, language, project == null? null :project.BaseDirectory, name); Stream stream = templ.CreateFileContent(policyParent, project, language, fileName, identifier); StreamReader reader = new StreamReader(stream); tags ["CodeRegion:" + regionName] = reader.ReadToEnd(); reader.Close(); stream.Close(); } }
// public IMember AddMember (IType cls, CodeTypeMember member) // { // RefactorerContext gctx = GetGeneratorContext (cls); // IRefactorer gen = GetGeneratorForClass (cls); // IMember m = gen.AddMember (gctx, cls, member); // gctx.Save (); // return m; // } // // public IType AddMembers (IType cls, IEnumerable<CodeTypeMember> members, string foldingRegionName) // { // RefactorerContext gctx = GetGeneratorContext (cls); // IRefactorer gen = GetGeneratorForClass (cls); // gen.AddMembers (gctx, cls, members, foldingRegionName); // gctx.Save (); // return GetUpdatedClass (gctx, cls); // } // public IMember ImplementMember (IType cls, IMember member, IReturnType privateReturnType) // { // RefactorerContext gctx = GetGeneratorContext (cls); // IRefactorer gen = GetGeneratorForClass (cls); // IMember m = gen.ImplementMember (gctx, cls, member, privateReturnType); // gctx.Save (); // return m; // } public void AddGlobalNamespaceImport(ProjectDom dom, string fileName, string nsName) { IRefactorer refactorer = LanguageBindingService.GetRefactorerForFile(fileName); refactorer.AddGlobalNamespaceImport(new RefactorerContext(dom, fileProvider, null), fileName, nsName); }
/// <summary> /// Load a single project as solution. /// </summary> public static void LoadProject(string fileName) { if (!Path.IsPathRooted(fileName)) { throw new ArgumentException("Path must be rooted!"); } string solutionFile = Path.ChangeExtension(fileName, ".sln"); if (File.Exists(solutionFile)) { LoadSolution(solutionFile); if (openSolution != null) { bool found = false; foreach (IProject p in openSolution.Projects) { if (FileUtility.IsEqualFileName(fileName, p.FileName)) { found = true; break; } } if (found == false) { string[,] parseArgs = { { "SolutionName", Path.GetFileName(solutionFile) }, { "ProjectName", Path.GetFileName(fileName) } }; int res = MessageService.ShowCustomDialog(MessageService.ProductName, StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.OpenCombine.SolutionDoesNotContainProject}", parseArgs), 0, 2, StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.OpenCombine.SolutionDoesNotContainProject.AddProjectToSolution}", parseArgs), StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.OpenCombine.SolutionDoesNotContainProject.CreateNewSolution}", parseArgs), "${res:Global.IgnoreButtonText}"); if (res == 0) { // Add project to solution Commands.AddExitingProjectToSolution.AddProject((ISolutionFolderNode)ProjectBrowserPad.Instance.SolutionNode, fileName); SaveSolution(); return; } else if (res == 1) { CloseSolution(); try { File.Copy(solutionFile, Path.ChangeExtension(solutionFile, ".old.sln"), true); } catch (IOException) {} } else { // ignore, just open the solution return; } } else { // opened solution instead and correctly found the project return; } } else { // some problem during opening, abort return; } } Solution solution = new Solution(); solution.Name = Path.GetFileNameWithoutExtension(fileName); ILanguageBinding binding = LanguageBindingService.GetBindingPerProjectFile(fileName); IProject project; if (binding != null) { project = LanguageBindingService.LoadProject(solution, fileName, solution.Name); if (project is UnknownProject) { if (((UnknownProject)project).WarningDisplayedToUser == false) { ((UnknownProject)project).ShowWarningMessageBox(); } return; } } else { MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.OpenCombine.InvalidProjectOrCombine}", new string[, ] { { "FileName", fileName } })); return; } solution.AddFolder(project); ProjectSection configSection = solution.GetSolutionConfigurationsSection(); foreach (string configuration in project.ConfigurationNames) { foreach (string platform in project.PlatformNames) { string key; if (platform == "AnyCPU") // Fix for SD2-786 { key = configuration + "|Any CPU"; } else { key = configuration + "|" + platform; } configSection.Items.Add(new SolutionItem(key, key)); } } solution.FixSolutionConfiguration(new IProject[] { project }); if (FileUtility.ObservedSave((NamedFileOperationDelegate)solution.Save, solutionFile) == FileOperationResult.OK) { // only load when saved succesfully LoadSolution(solutionFile); } }
public override bool IsValidInContext(DocumentContext context) { return(LanguageBindingService.GetBindingPerFileName(context.Name) != null); }
public IProject CreateProject(ProjectCreateInformation projectCreateInformation, string defaultLanguage) { // remember old outerProjectBasePath string outerProjectBasePath = projectCreateInformation.ProjectBasePath; string outerProjectName = projectCreateInformation.ProjectName; try { projectCreateInformation.ProjectBasePath = Path.Combine(projectCreateInformation.ProjectBasePath, this.relativePath); if (!Directory.Exists(projectCreateInformation.ProjectBasePath)) { Directory.CreateDirectory(projectCreateInformation.ProjectBasePath); } string language = string.IsNullOrEmpty(languageName) ? defaultLanguage : languageName; LanguageBindingDescriptor descriptor = LanguageBindingService.GetCodonPerLanguageName(language); ILanguageBinding languageinfo = (descriptor != null) ? descriptor.Binding : null; if (languageinfo == null) { StringParser.Properties["type"] = language; MessageService.ShowError("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.CantCreateProjectWithTypeError}"); return(null); } string newProjectName = StringParser.Parse(name, new string[, ] { { "ProjectName", projectCreateInformation.ProjectName } }); string projectLocation = Path.GetFullPath(Path.Combine(projectCreateInformation.ProjectBasePath, newProjectName + LanguageBindingService.GetProjectFileExtension(language))); StringBuilder standardNamespace = new StringBuilder(); // filter 'illegal' chars from standard namespace if (newProjectName != null && newProjectName.Length > 0) { char ch = '.'; for (int i = 0; i < newProjectName.Length; ++i) { if (ch == '.') { // at beginning or after '.', only a letter or '_' is allowed ch = newProjectName[i]; if (!Char.IsLetter(ch)) { standardNamespace.Append('_'); } else { standardNamespace.Append(ch); } } else { ch = newProjectName[i]; // can only contain letters, digits or '_' if (!Char.IsLetterOrDigit(ch) && ch != '.') { standardNamespace.Append('_'); } else { standardNamespace.Append(ch); } } } } projectCreateInformation.OutputProjectFileName = projectLocation; projectCreateInformation.RootNamespace = standardNamespace.ToString(); projectCreateInformation.ProjectName = newProjectName; StringParser.Properties["StandardNamespace"] = projectCreateInformation.RootNamespace; IProject project; try { project = languageinfo.CreateProject(projectCreateInformation); } catch (ProjectLoadException ex) { MessageService.ShowError(ex.Message); return(null); } // Add Project items foreach (ProjectItem projectItem in projectItems) { ProjectItem newProjectItem = new UnknownProjectItem( project, StringParser.Parse(projectItem.ItemType.ItemName), StringParser.Parse(projectItem.Include) ); foreach (string metadataName in projectItem.MetadataNames) { newProjectItem.SetEvaluatedMetadata( StringParser.Parse(metadataName), StringParser.Parse(projectItem.GetMetadata(metadataName)) ); } ((IProjectItemListProvider)project).AddProjectItem(newProjectItem); } // Add Imports if (clearExistingImports || projectImports.Count > 0) { if (!(project is MSBuildBasedProject)) { throw new Exception("<Imports> may be only used in project templates for MSBuildBasedProjects"); } if (clearExistingImports) { MSBuildInternals.ClearImports(((MSBuildBasedProject)project).MSBuildProject); } try { foreach (Import projectImport in projectImports) { ((MSBuildBasedProject)project).MSBuildProject.AddNewImport(projectImport.Key, projectImport.Value); } ((MSBuildBasedProject)project).CreateItemsListFromMSBuild(); } catch (MSBuild.InvalidProjectFileException ex) { if (string.IsNullOrEmpty(importsFailureMessage)) { MessageService.ShowError("Error creating project:\n" + ex.Message); } else { MessageService.ShowError(importsFailureMessage + "\n\n" + ex.Message); } return(null); } } if (projectProperties.Count > 0) { if (!(project is MSBuildBasedProject)) { throw new Exception("<PropertyGroup> may be only used in project templates for MSBuildBasedProjects"); } foreach (ProjectProperty p in projectProperties) { ((MSBuildBasedProject)project).SetProperty( StringParser.Parse(p.Configuration), StringParser.Parse(p.Platform), StringParser.Parse(p.Name), StringParser.Parse(p.Value), p.Location, p.ValueIsLiteral ); } } // Add Files foreach (FileDescriptionTemplate file in files) { string fileName = Path.Combine(projectCreateInformation.ProjectBasePath, StringParser.Parse(file.Name, new string[, ] { { "ProjectName", projectCreateInformation.ProjectName } })); FileProjectItem projectFile = new FileProjectItem(project, project.GetDefaultItemType(fileName)); projectFile.Include = FileUtility.GetRelativePath(project.Directory, fileName); file.SetProjectItemProperties(projectFile); ((IProjectItemListProvider)project).AddProjectItem(projectFile); if (File.Exists(fileName)) { StringParser.Properties["fileName"] = fileName; if (!MessageService.AskQuestion("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion}", "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion.InfoName}")) { continue; } } try { if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } if (file.ContentData != null) { // Binary content File.WriteAllBytes(fileName, file.ContentData); } else { // Textual content StreamWriter sr = new StreamWriter(File.Create(fileName), ParserService.DefaultFileEncoding); string fileContent = StringParser.Parse(file.Content, new string[, ] { { "ProjectName", projectCreateInformation.ProjectName }, { "FileName", fileName } }); fileContent = StringParser.Parse(fileContent); if (SharpDevelopTextEditorProperties.Instance.IndentationString != "\t") { fileContent = fileContent.Replace("\t", SharpDevelopTextEditorProperties.Instance.IndentationString); } sr.Write(fileContent); sr.Close(); } } catch (Exception ex) { StringParser.Properties["fileName"] = fileName; MessageService.ShowError(ex, "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.FileCouldntBeWrittenError}"); } } RunCreateActions(project); // Save project if (File.Exists(projectLocation)) { StringParser.Properties["projectLocation"] = projectLocation; if (MessageService.AskQuestion("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteProjectQuestion}", "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion.InfoName}")) { project.Save(); } } else { project.Save(); } projectCreateInformation.createdProjects.Add(project); ProjectService.OnProjectCreated(new ProjectEventArgs(project)); return(project); } finally { // set back outerProjectBasePath projectCreateInformation.ProjectBasePath = outerProjectBasePath; projectCreateInformation.ProjectName = outerProjectName; } }
public bool CanCreateSingleFileProject(string file) { IDotNetLanguageBinding binding = LanguageBindingService.GetBindingPerFileName(file) as IDotNetLanguageBinding; return(binding != null); }
MethodExtractorBase GetCurrentExtractor(TextEditorControl editor) { switch (LanguageBindingService.GetCodonPerCodeFileName(editor.FileName).Language) { case "C#": return(new CSharpMethodExtractor(editor, editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0])); default: MessageService.ShowError(string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ExtractMethodNotSupported}"), LanguageBindingService.GetCodonPerCodeFileName(editor.FileName).Language)); return(null); } }
public override bool ExtendsEditor(Document doc, IEditableTextBuffer editor) { var binding = LanguageBindingService.GetBindingPerFileName(doc.Name); return(binding != null && binding is IDotNetLanguageBinding); }
public static void AddProject(ISolutionFolderNode solutionFolderNode, string fileName) { AddProject(solutionFolderNode, LanguageBindingService.LoadProject(solutionFolderNode.Solution, fileName, Path.GetFileNameWithoutExtension(fileName))); }
void Refactor(IProgressMonitor monitor, IType cls, RefactoryScope scope, RefactorDelegate refactorDelegate) { switch (scope) { case RefactoryScope.DeclaringType: ProjectDom ctx = GetParserContext(cls); if (cls is InstantiatedType) { cls = ((InstantiatedType)cls).UninstantiatedType; } IType resolvedType = ctx.GetType(cls.FullName, cls.TypeParameters.Count, true, true); if (resolvedType == null) { goto case RefactoryScope.Solution; } foreach (IType part in resolvedType.Parts) { string file = part.CompilationUnit.FileName; RefactorerContext gctx = GetGeneratorContext(part); IRefactorer gen = LanguageBindingService.GetRefactorerForFile(file); if (gen == null) { return; } refactorDelegate(monitor, gctx, gen, file); gctx.Save(); } break; case RefactoryScope.File: { string file = cls.CompilationUnit.FileName; RefactorerContext gctx = GetGeneratorContext(cls); IRefactorer gen = LanguageBindingService.GetRefactorerForFile(file); if (gen == null) { return; } refactorDelegate(monitor, gctx, gen, file); gctx.Save(); break; } case RefactoryScope.Project: Project prj = GetProjectForFile(cls.CompilationUnit.FileName); if (prj == null) { return; } RefactorProject(monitor, prj, refactorDelegate); break; case RefactoryScope.Solution: if (solution == null) { goto case RefactoryScope.File; } foreach (Project project in solution.GetAllProjects()) { RefactorProject(monitor, project, refactorDelegate); } break; } }
public DomLocation CompleteStatement(ProjectDom dom, string fileName, DomLocation caretLocation) { IRefactorer refactorer = LanguageBindingService.GetRefactorerForFile(fileName); return(refactorer.CompleteStatement(new RefactorerContext(dom, fileProvider, null), fileName, caretLocation)); }
IRefactorer GetGeneratorForVariable(LocalVariable var) { return(LanguageBindingService.GetRefactorerForFile(var.FileName)); }
IRefactorer GetGeneratorForClass(IType cls) { return(LanguageBindingService.GetRefactorerForFile(cls.CompilationUnit.FileName)); }