📄 projectservice.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 1417 $</version>
// </file>
using System;
using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
namespace ICSharpCode.SharpDevelop.Project
{
public static class ProjectService
{
static Solution openSolution;
static IProject currentProject;
public static Solution OpenSolution {
[System.Diagnostics.DebuggerStepThrough]
get {
return openSolution;
}
}
public static IProject CurrentProject {
[System.Diagnostics.DebuggerStepThrough]
get {
return currentProject;
}
set {
if (currentProject != value) {
LoggingService.Info("CurrentProject changed to " + (value == null ? "null" : value.Name));
currentProject = value;
OnCurrentProjectChanged(new ProjectEventArgs(currentProject));
}
}
}
/// <summary>
/// Gets an open project by the name of the project file.
/// </summary>
public static IProject GetProject(string projectFilename)
{
if (openSolution == null) return null;
foreach (IProject project in openSolution.Projects) {
if (FileUtility.IsEqualFileName(project.FileName, projectFilename)) {
return project;
}
}
return null;
}
static bool initialized;
public static void InitializeService()
{
if (initialized)
throw new InvalidOperationException("ProjectService already is initialized");
initialized = true;
WorkbenchSingleton.Workbench.ActiveWorkbenchWindowChanged += ActiveWindowChanged;
FileService.FileRenamed += FileServiceFileRenamed;
FileService.FileRemoved += FileServiceFileRemoved;
}
public static IProjectLoader GetProjectLoader(string fileName)
{
AddInTreeNode addinTreeNode = AddInTree.GetTreeNode("/SharpDevelop/Workbench/Combine/FileFilter");
foreach (Codon codon in addinTreeNode.Codons) {
string pattern = codon.Properties.Get("extensions", "");
if (FileUtility.MatchesPattern(fileName, pattern) && codon.Properties.Contains("class")) {
object binding = codon.AddIn.CreateObject(codon.Properties["class"]);
return binding as IProjectLoader;
}
}
return null;
}
public static void LoadSolutionOrProject(string fileName)
{
IProjectLoader loader = GetProjectLoader(fileName);
if (loader != null) {
loader.Load(fileName);
} else {
MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.OpenCombine.InvalidProjectOrCombine}", new string[,] {{"FileName", fileName}}));
}
}
static void FileServiceFileRenamed(object sender, FileRenameEventArgs e)
{
if (OpenSolution == null) {
return;
}
string oldName = e.SourceFile;
string newName = e.TargetFile;
long x = 0;
foreach (ISolutionFolderContainer container in OpenSolution.SolutionFolderContainers) {
foreach (SolutionItem item in container.SolutionItems.Items) {
string oldFullName = Path.Combine(OpenSolution.Directory, item.Name);
++x;
if (FileUtility.IsBaseDirectory(oldName, oldFullName)) {
string newFullName = FileUtility.RenameBaseDirectory(oldFullName, oldName, newName);
item.Name = item.Location = FileUtility.GetRelativePath(OpenSolution.Directory, newFullName);
}
}
}
long y = 0;
foreach (IProject project in OpenSolution.Projects) {
if (FileUtility.IsBaseDirectory(project.Directory, oldName)) {
foreach (ProjectItem item in project.Items) {
++y;
if (FileUtility.IsBaseDirectory(oldName, item.FileName)) {
OnProjectItemRemoved(new ProjectItemEventArgs(project, item));
item.FileName = FileUtility.RenameBaseDirectory(item.FileName, oldName, newName);
OnProjectItemAdded(new ProjectItemEventArgs(project, item));
}
}
}
}
}
static void FileServiceFileRemoved(object sender, FileEventArgs e)
{
if (OpenSolution == null) {
return;
}
string fileName = e.FileName;
foreach (ISolutionFolderContainer container in OpenSolution.SolutionFolderContainers) {
for (int i = 0; i < container.SolutionItems.Items.Count;) {
SolutionItem item = container.SolutionItems.Items[i];
if (FileUtility.IsBaseDirectory(fileName, Path.Combine(OpenSolution.Directory, item.Name))) {
container.SolutionItems.Items.RemoveAt(i);
} else {
++i;
}
}
}
foreach (IProject project in OpenSolution.Projects) {
if (FileUtility.IsBaseDirectory(project.Directory, fileName)) {
for (int i = 0; i < project.Items.Count;) {
ProjectItem item =project.Items[i];
if (FileUtility.IsBaseDirectory(fileName, item.FileName)) {
project.Items.RemoveAt(i);
OnProjectItemRemoved(new ProjectItemEventArgs(project, item));
} else {
++i;
}
}
}
}
}
static void ActiveWindowChanged(object sender, EventArgs e)
{
object activeContent = WorkbenchSingleton.Workbench.ActiveContent;
IViewContent viewContent = activeContent as IViewContent;
if (viewContent == null && activeContent is ISecondaryViewContent) {
// required if one creates a new winforms app and then immediately switches to design mode
// without focussing the text editor
IWorkbenchWindow window = ((ISecondaryViewContent)activeContent).WorkbenchWindow;
if (window == null) // workbench window is being disposed
return;
viewContent = window.ViewContent;
}
if (OpenSolution == null || viewContent == null) {
return;
}
string fileName = viewContent.FileName;
if (fileName == null) {
return;
}
CurrentProject = OpenSolution.FindProjectContainingFile(fileName) ?? CurrentProject;
}
public static void AddProject(ISolutionFolderNode solutionFolderNode, IProject newProject)
{
solutionFolderNode.Container.AddFolder(newProject);
ParserService.CreateProjectContentForAddedProject(newProject);
solutionFolderNode.Solution.FixSolutionConfiguration(new IProject[] { newProject });
OnProjectAdded(new ProjectEventArgs(newProject));
}
/// <summary>
/// Adds a project item to the project, raising the ProjectItemAdded event.
/// Make sure you call project.Save() after adding new items!
/// </summary>
public static void AddProjectItem(IProject project, ProjectItem item)
{
if (project == null) throw new ArgumentNullException("project");
if (item == null) throw new ArgumentNullException("item");
project.Items.Add(item);
OnProjectItemAdded(new ProjectItemEventArgs(project, item));
}
/// <summary>
/// Removes a project item from the project, raising the ProjectItemRemoved event.
/// Make sure you call project.Save() after removing items!
/// </summary>
public static void RemoveProjectItem(IProject project, ProjectItem item)
{
if (project == null) throw new ArgumentNullException("project");
if (item == null) throw new ArgumentNullException("item");
if (!project.Items.Remove(item)) {
throw new ArgumentException("The item was not found in the project!");
}
OnProjectItemRemoved(new ProjectItemEventArgs(project, item));
}
static void BeforeLoadSolution()
{
if (openSolution != null) {
SaveSolutionPreferences();
WorkbenchSingleton.Workbench.CloseAllViews();
CloseSolution();
}
}
public static void LoadSolution(string fileName)
{
BeforeLoadSolution();
try {
openSolution = Solution.Load(fileName);
if (openSolution == null)
return;
} catch (UnauthorizedAccessException ex) {
MessageService.ShowError(ex.Message);
return;
}
AbstractProject.filesToOpenAfterSolutionLoad.Clear();
try {
string file = GetPreferenceFileName(openSolution.FileName);
if (FileUtility.IsValidFileName(file) && File.Exists(file)) {
(openSolution.Preferences as IMementoCapable).SetMemento(Properties.Load(file));
} else {
(openSolution.Preferences as IMementoCapable).SetMemento(new Properties());
}
ApplyConfigurationAndReadPreferences();
} catch (Exception ex) {
MessageService.ShowError(ex);
}
// Create project contents for solution
ParserService.OnSolutionLoaded();
// preferences must be read before OnSolutionLoad is called to enable
// the event listeners to read e.Solution.Preferences.Properties
OnSolutionLoaded(new SolutionEventArgs(openSolution));
}
internal static void ParserServiceCreatedProjectContents()
{
foreach (string file in AbstractProject.filesToOpenAfterSolutionLoad) {
if (File.Exists(file)) {
FileService.OpenFile(file);
}
}
AbstractProject.filesToOpenAfterSolutionLoad.Clear();
}
static void ApplyConfigurationAndReadPreferences()
{
openSolution.ApplySolutionConfigurationToProjects();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -