"All documents should be commited"
I am writing to a file from the solution in the ExecuteInternal of a context action. It works fine without any errors but if I go look at the file to see the changes, any subsequent usages of the context action gives me the error "All documents should be commited, but these are dirty"
I am finding the IDocument like this:
FileSystemPath directory = Provider.Solution.SolutionFilePath.Directory;
IList<IProjectItem> projectItems = Provider.Solution.FindProjectItemsByLocation(directory.Combine("myFile.xml"));
foreach (var projectItem in projectItems)
{
if (projectItem.Name == "myFile.xml")
{
{
if (projectItem.Name == "myFile.xml")
{
IProjectFile projectFile = projectItem as IProjectFile;
if (projectFile != null)
{
IDocument document = DocumentManager.GetInstance(Provider.Solution).GetDocument(projectFile);
if (projectFile != null)
{
IDocument document = DocumentManager.GetInstance(Provider.Solution).GetDocument(projectFile);
using (CommandCookie.Create(NAME))
{
using (ModificationCookie cookie = document.EnsureWritable())
{
if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
{
return;
}
{
using (ModificationCookie cookie = document.EnsureWritable())
{
if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
{
return;
}
using (WriteLockCookie.Create())
{
// in here I find the line number I want to insert into and then...
document.InsertText(document.GetLineStartOffset(insertionLine + 1), componentEntry + Environment.NewLine);
}
....
In desperation I have tried adding Provider.PsiManager.CommitAllDocuments(); in both ExecuteInternalPostPSITransaction and ExecuteBeforeTransaction but I still have the same error message when I try to execute the action after viewing the myFile.xml file in visual studio.
Thanks
{
// in here I find the line number I want to insert into and then...
document.InsertText(document.GetLineStartOffset(insertionLine + 1), componentEntry + Environment.NewLine);
}
....
In desperation I have tried adding Provider.PsiManager.CommitAllDocuments(); in both ExecuteInternalPostPSITransaction and ExecuteBeforeTransaction but I still have the same error message when I try to execute the action after viewing the myFile.xml file in visual studio.
Thanks
Please sign in to leave a comment.
OK I changed my base class from OneItemContextActionBase to BulbItemImpl and implemented IContextAction. I also removed the WriteLock. Now it works perfectly except sometimes (I don't know why it happens sometimes only) VisualStudio will crash as soon as I click on the tab for the file that the context action has changed.
so now my change block looks like this:
using (CommandCookie.Create(NAME))
{
using (ModificationCookie cookie = document.EnsureWritable())
{
if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
{
return;
}
int insertionLine = GetInsertionLine(document);
document.InsertText(document.GetLineStartOffset(insertionLine + 1), componentEntry + Environment.NewLine);
}
}
and that's being called from
Action<ITextControl> ExecuteTransaction(ISolution solution, IProgressIndicator progress)
from BulbItemImpl
I just need to prevent the random crash of VS now