0
我有以下組件:IntelliJ IDEA的插件持久狀態XML有重複的行
@State(
name = "SessionConfiguration",
storages = {
@Storage(id = "default", file = StoragePathMacros.PROJECT_FILE),
@Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/tabsession.xml", scheme = StorageScheme.DIRECTORY_BASED)
}
)
public class SessionComponent implements ProjectComponent, PersistentStateComponent<Element> {
...
@Nullable
@Override
public Element getState() {
LOG.debug("Saving state");
final Element element = new Element("tabsession");
// meta configuration information
final Element stateElement = new Element("state");
stateElement.setAttribute("version", Integer.toString(STATE_VERSION));
element.addContent(stateElement);
// session data
final Element sessionsElement = new Element("sessions");
for(SessionState.Session session : sessionState.sessions) {
final Element sessionElement = new Element("session");
sessionElement.setAttribute("name", session.name);
final Element filesElement = new Element("files");
for(String path : session.files) {
final Element fileElement = new Element("file");
fileElement.setAttribute("path", path);
filesElement.addContent(fileElement);
}
filesElement.setAttribute("focusedFile", session.focusedFile);
sessionElement.addContent(filesElement);
sessionsElement.addContent(sessionElement);
}
element.addContent(sessionsElement);
return element;
}
...
}
這工作得很好,並生成目標文件tabsession.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SessionConfiguration">
<state version="1" />
<sessions>
<session name="first">
<files focusedFile="$PROJECT_DIR$/src/First.java">
<file path="$PROJECT_DIR$/src/First.java" />
<file path="$PROJECT_DIR$/src/Second.java" />
</files>
</session>
</sessions>
</component>
</project>
但如果狀態就會再次保存用不同的數據,舊線不會被刪除。例如,如果我打開另一個文件,狀態就會保存在XML看起來是這樣的:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SessionConfiguration">
<state version="1" />
<sessions>
<session name="first">
<files focusedFile="$PROJECT_DIR$/src/First.java">
<file path="$PROJECT_DIR$/src/First.java" />
<file path="$PROJECT_DIR$/src/Second.java" />
<file path="$PROJECT_DIR$/src/First.java" />
<file path="$PROJECT_DIR$/src/Second.java" />
<file path="$PROJECT_DIR$/src/Third.java" />
</files>
</session>
</sessions>
</component>
</project>
這顯然是錯誤的。任何想法如何我可以保存只有當前狀態,同時刪除舊線?