// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Refactoring;
namespace ICSharpCode.SharpDevelop.Parser
{
///
/// Manages parse runs and caches ParseInformation.
///
[SDService]
public interface IParserService
{
///
/// Gets/Sets the task list tokens.
/// The getter of this property is thread-safe;
/// the setter must only be called on the main thread.
///
IReadOnlyList TaskListTokens { get; set; }
ILoadSolutionProjectsThread LoadSolutionProjectsThread { get; }
#region GetCompilation
///
/// Gets or creates a compilation for the specified project.
///
///
/// This method is thread-safe.
/// This method never returns null - in case of errors, a dummy compilation is created.
///
ICompilation GetCompilation(IProject project);
///
/// Gets or creates a compilation for the project that contains the specified file.
///
///
/// This method is thread-safe.
/// This method never returns null - in case of errors, a dummy compilation is created.
///
ICompilation GetCompilationForFile(FileName fileName);
///
/// Gets a snapshot of the current compilations
/// This method is useful when a consistent snapshot across multiple compilations is needed.
///
///
/// This method is thread-safe.
///
SharpDevelopSolutionSnapshot GetCurrentSolutionSnapshot();
///
/// Invalidates the current solution snapshot, causing
/// the next call to create a new one.
/// This method needs to be called whenever IProject.ProjectContent changes.
///
///
/// This method is thread-safe.
///
void InvalidateCurrentSolutionSnapshot();
#endregion
#region GetExistingUnresolvedFile
///
/// Gets the unresolved type system for the specified file.
///
/// Name of the file.
///
/// Optional: requested version of the file.
/// If this parameter is specified and the existing parsed file belongs to a different version,
/// this method will return null.
///
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
/// Returns the IUnresolvedFile for the specified file,
/// or null if the file has not been parsed yet.
///
/// This method is thread-safe.
IUnresolvedFile GetExistingUnresolvedFile(FileName fileName, ITextSourceVersion version = null, IProject parentProject = null);
///
/// Gets full parse information for the specified file, if it is available.
///
/// Name of the file.
///
/// Optional: requested version of the file.
/// If this parameter is specified and the existing parsed file belongs to a different version,
/// this method will return null.
///
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
/// If only the IUnresolvedFile is available (non-full parse information), this method returns null.
///
ParseInformation GetCachedParseInformation(FileName fileName, ITextSourceVersion version = null, IProject parentProject = null);
#endregion
#region Parse
///
/// Parses the specified file.
/// Produces full parse information.
///
/// Name of the file to parse
/// Optional: Content of the file to parse.
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
/// Returns the ParseInformation for the specified file, or null if the file cannot be parsed.
/// For files currently open in an editor, this method does not necessary reparse, but may return
/// an existing cached parse information (but only if it's still up-to-date).
///
///
/// This method is thread-safe.
///
/// If is null, this method will block and wait for the main thread
/// to retrieve the latest file content. This can cause deadlocks if this method is called within a lock.
///
///
/// If not null, the exact file version specified will be parsed.
/// This method will not wait for the main thread in that case.
/// If the specified version is older than the latest version, the old version will be parsed
/// and returned, but the old parse information will not be registered.
///
///
ParseInformation Parse(FileName fileName, ITextSource fileContent = null, IProject parentProject = null,
CancellationToken cancellationToken = default(CancellationToken));
///
/// Parses the specified file.
/// This method does not request full parse information.
///
/// Name of the file to parse
/// Optional: Content of the file to parse.
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
/// Returns the IUnresolvedFile for the specified file, or null if the file cannot be parsed.
/// For files currently open in an editor, this method does not necessarily reparse, but may return
/// the existing IUnresolvedFile (but only if it's still up-to-date).
///
///
IUnresolvedFile ParseFile(FileName fileName, ITextSource fileContent = null, IProject parentProject = null,
CancellationToken cancellationToken = default(CancellationToken));
///
/// Parses the specified file on a background thread.
/// Produces full parse information.
///
/// Name of the file to parse
/// Optional: Content of the file to parse.
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
///
/// This method is thread-safe.
///
/// If is null, the task wait for the main thread
/// to retrieve the latest file content.
/// This means that waiting for the task can cause deadlocks. (however, using C# 5 await is safe)
///
///
/// If not null, the exact file version specified will be parsed.
/// This method will not wait for the main thread in that case.
/// If the specified version is older than the latest version, the old version will be parsed
/// and returned, but the old parse information will not be registered.
///
///
Task ParseAsync(FileName fileName, ITextSource fileContent = null, IProject parentProject = null,
CancellationToken cancellationToken = default(CancellationToken));
///
/// Parses the specified file on a background thread.
/// This method does not request full parse information.
///
/// Name of the file to parse
/// Optional: Content of the file to parse.
///
/// Optional: If the file is part of multiple projects, specifies
/// which parsed version of the file to return (for example, different project settings
/// can cause the file to be parsed differently).
///
///
///
Task ParseFileAsync(FileName fileName, ITextSource fileContent = null, IProject parentProject = null,
CancellationToken cancellationToken = default(CancellationToken));
#endregion
#region Resolve
ResolveResult Resolve(ITextEditor editor, TextLocation location,
ICompilation compilation = null,
CancellationToken cancellationToken = default(CancellationToken));
ResolveResult Resolve(FileName fileName, TextLocation location,
ITextSource fileContent = null, ICompilation compilation = null,
CancellationToken cancellationToken = default(CancellationToken));
ResolveResult ResolveSnippet(FileName fileName, TextLocation fileLocation,
ITextSource fileContent, string codeSnippet, ICompilation compilation,
CancellationToken cancellationToken);
Task ResolveAsync(FileName fileName, TextLocation location,
ITextSource fileContent = null, ICompilation compilation = null,
CancellationToken cancellationToken = default(CancellationToken));
Task FindLocalReferencesAsync(FileName fileName, IVariable variable, Action callback,
ITextSource fileContent = null, ICompilation compilation = null,
CancellationToken cancellationToken = default(CancellationToken));
#endregion
#region Parsed File Listeners
///
/// Gets whether a parser is registered for the specified file name.
///
bool HasParser(FileName fileName);
///
/// Clears the cached parse information for the specified file.
/// If the file does not belong to any project, this also clears the cached type system.
/// If the file belongs to a project, the cached type system is kept (so that the classes don't go missing from the project),
/// but the next ParseFile() call will cause it to be reparsed even if it is unchanged.
///
void ClearParseInformation(FileName fileName);
///
/// Adds a project that owns the file and wishes to receive parse information.
///
/// Name of the file contained in the project.
/// The parent project of the file.
///
/// Whether to start an asynchronous parse operation for the specified file.
///
///
/// Specified whether the file is linked within the project, i.e. likely also belongs to another project.
/// The parser services tries to use the project that contains the file directly (non-linked)
/// as the primary parent project.
///
void AddOwnerProject(FileName fileName, IProject project, bool startAsyncParse, bool isLinkedFile);
///
/// Removes a project from the owners of the file.
/// This method invokes project.UpdateParseInformation(existingUnresolvedFile, null);.
/// (unless existingUnresolvedFile==null)
///
void RemoveOwnerProject(FileName fileName, IProject project);
///
/// Occurs whenever parse information was updated. This event is raised on the main thread.
///
event EventHandler ParseInformationUpdated;
#endregion
///
/// Registers parse information for the specified file.
/// The file must belong to the specified project, otherwise this method does nothing.
///
/// This method is intended for restoring parse information cached on disk.
void RegisterUnresolvedFile(FileName fileName, IProject project, IUnresolvedFile unresolvedFile);
}
public interface ILoadSolutionProjectsThread
{
///
/// Gets whether the solution is being loaded, or a major re-parse is happening
/// (e.g. after adding a project).
///
bool IsRunning { get; }
///
/// This event is raised when the IsRunning property changes to true.
/// This always happens on the main thread.
///
event EventHandler Started;
///
/// This event is raised when the IsRunning property changes to false.
/// This always happens on the main thread.
///
event EventHandler Finished;
///
/// Adds a new task to the job queue, and starts the LoadSolutionProjects thread (if its not already running).
///
/// The action to run. Parameter: a nested progress monitor for the action.
/// Name of the action - shown in the status bar
/// Cost of the action
void AddJob(Action action, string name, double cost);
}
}