use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class AbstractConvertLineSeparatorsAction method changeLineSeparators.
public static void changeLineSeparators(@NotNull final Project project, @NotNull final VirtualFile virtualFile, @NotNull final String newSeparator) {
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Document document = fileDocumentManager.getCachedDocument(virtualFile);
if (document != null) {
fileDocumentManager.saveDocument(document);
}
String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false);
final String commandText;
if (StringUtil.isEmpty(currentSeparator)) {
commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator);
} else {
commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator), LineSeparator.fromString(newSeparator));
}
new WriteCommandAction(project, commandText) {
@Override
protected void run(@NotNull Result result) throws Throwable {
try {
LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this);
} catch (IOException e) {
LOG.info(e);
}
}
}.execute();
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.
the class TsLintExternalAnnotator method saveConfigFileAndReturnSkipProcessing.
public boolean saveConfigFileAndReturnSkipProcessing(@NotNull Project project, @NotNull VirtualFile config) {
return ReadAction.compute(() -> {
final FileDocumentManager manager = FileDocumentManager.getInstance();
Document document = manager.getCachedDocument(config);
if (document != null) {
boolean unsaved = manager.isDocumentUnsaved(document);
if (unsaved) {
ApplicationManager.getApplication().invokeLater(() -> {
Document newDocument = manager.getCachedDocument(config);
if (newDocument != null) {
FileDocumentManager.getInstance().saveDocument(newDocument);
}
DaemonCodeAnalyzer.getInstance(project).restart();
}, project.getDisposed());
}
return unsaved;
}
return false;
});
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.
the class FlexCompilerConfigFileUtil method getInfoFromConfigFile.
@NotNull
public static InfoFromConfigFile getInfoFromConfigFile(final String configFilePath) {
final VirtualFile configFile = configFilePath.isEmpty() ? null : LocalFileSystem.getInstance().findFileByPath(configFilePath);
if (configFile == null) {
return InfoFromConfigFile.DEFAULT;
}
Pair<Long, InfoFromConfigFile> data = configFile.getUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document cachedDocument = documentManager.getCachedDocument(configFile);
final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : configFile.getModificationCount();
final Long cachedTimestamp = data == null ? null : data.first;
if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
data = null;
configFile.putUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE, data);
final List<String> xmlElements = Arrays.asList(FILE_SPEC_ELEMENT, OUTPUT_ELEMENT, TARGET_PLAYER_ELEMENT);
String mainClassPath = null;
String outputPath = null;
String targetPlayer = null;
try {
final InputStream inputStream = cachedDocument == null ? configFile.getInputStream() : new ByteArrayInputStream(cachedDocument.getText().getBytes());
final Map<String, List<String>> map = FlexUtils.findXMLElements(inputStream, xmlElements);
final List<String> fileSpecList = map.get(FILE_SPEC_ELEMENT);
if (!fileSpecList.isEmpty()) {
mainClassPath = fileSpecList.get(0).trim();
}
final List<String> outputList = map.get(OUTPUT_ELEMENT);
if (!outputList.isEmpty()) {
outputPath = outputList.get(0).trim();
if (!FileUtil.isAbsolute(outputPath)) {
try {
outputPath = FileUtil.toSystemIndependentName(new File(configFile.getParent().getPath() + "/" + outputPath).getCanonicalPath());
} catch (IOException e) {
outputPath = FileUtil.toSystemIndependentName(new File(configFile.getParent().getPath() + "/" + outputPath).getAbsolutePath());
}
}
}
final List<String> targetPlayerList = map.get(TARGET_PLAYER_ELEMENT);
if (!targetPlayerList.isEmpty()) {
targetPlayer = targetPlayerList.get(0).trim();
}
} catch (IOException ignore) {
/*ignore*/
}
final String outputFileName = outputPath == null ? null : PathUtil.getFileName(outputPath);
final String outputFolderPath = outputPath == null ? null : PathUtil.getParentPath(outputPath);
data = Pair.create(currentTimestamp, new InfoFromConfigFile(configFile, mainClassPath, outputFileName, outputFolderPath, targetPlayer));
configFile.putUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE, data);
}
return data.second;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project ServiceStack.Java by ServiceStack.
the class IDEAPomFileHelper method PomAppendDependency.
private void PomAppendDependency(final Module module, final File pomFile, String groupId, String packageId, String version) throws ParserConfigurationException, IOException, SAXException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
final Document doc = docBuilder.parse(pomFile);
Node root = doc.getFirstChild();
if (!pomHasDependenciesNode(doc)) {
root.appendChild(doc.createElement("dependencies"));
}
Node dependenciesNode = getMavenDependenciesNode(doc);
Node newDepNode = doc.createElement("dependency");
Node groupNode = doc.createElement("groupId");
groupNode.appendChild(doc.createTextNode(groupId));
Node artifactNode = doc.createElement("artifactId");
artifactNode.appendChild(doc.createTextNode(packageId));
Node versionNode = doc.createElement("version");
versionNode.appendChild(doc.createTextNode(version));
newDepNode.appendChild(groupNode);
newDepNode.appendChild(artifactNode);
newDepNode.appendChild(versionNode);
dependenciesNode.appendChild(newDepNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
final StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
final Project project = module.getProject();
final VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(pomFile);
final com.intellij.openapi.editor.Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
// when file is edited and editor is closed, it is needed to save the text
fileDocumentManager.saveDocument(document);
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiDocumentManager.getInstance(module.getProject()).commitAllDocuments();
ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {
@Override
public void run() {
PsiDocumentManager.getInstance(module.getProject()).doPostponedOperationsAndUnblockDocument(document);
document.setText(writer.getBuffer().toString().replaceAll("\r\n", "\n"));
fileDocumentManager.saveDocument(document);
PsiDocumentManager.getInstance(project).commitDocument(document);
}
});
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.
the class EditorMouseListener method mousePressed.
@Override
public void mousePressed(EditorMouseEvent event) {
LOG.trace("EditorMouseListener#mousePressed({})", event);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Project project = event.getEditor().getProject();
DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
if (component != null)
component.updateTimeAccessed(documentManager.getFile(event.getEditor().getDocument()));
}
Aggregations