From fdd0f11743a90aae3c9095fff28bd1cd3614a30f Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 29 Feb 2016 13:54:39 +0300 Subject: [PATCH] removed core-impl dependency on properties files --- java/java-impl/java-impl.iml | 1 - platform/core-impl/core-impl.iml | 1 - .../openapi/fileEditor/impl/LoadTextUtil.java | 33 ++++++++----------- .../properties-psi-api/properties-psi-api.iml | 1 + .../lang/properties/PropertiesFileType.java | 11 +++++-- plugins/properties/properties.iml | 4 +-- .../ide/highlighter/HtmlFileType.java | 11 +++++-- .../ide/highlighter/XmlLikeFileType.java | 10 ++++-- 8 files changed, 42 insertions(+), 30 deletions(-) diff --git a/java/java-impl/java-impl.iml b/java/java-impl/java-impl.iml index 3a2e9d845261..d057bc913eb4 100644 --- a/java/java-impl/java-impl.iml +++ b/java/java-impl/java-impl.iml @@ -16,7 +16,6 @@ - diff --git a/platform/core-impl/core-impl.iml b/platform/core-impl/core-impl.iml index d2ef4900c6cc..c915b21d62ca 100644 --- a/platform/core-impl/core-impl.iml +++ b/platform/core-impl/core-impl.iml @@ -9,7 +9,6 @@ - diff --git a/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java b/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java index 9012dc726695..0dddcec2ffa3 100644 --- a/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java +++ b/platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ */ package com.intellij.openapi.fileEditor.impl; -import com.intellij.lang.properties.charset.Native2AsciiCharset; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ex.ApplicationUtil; @@ -119,33 +118,29 @@ public final class LoadTextUtil { } @NotNull - public static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { + private static Charset detectCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) { Charset charset = null; + String charsetName = fileType.getCharset(virtualFile, content); Trinity guessed = guessFromContent(virtualFile, content, content.length); - if (guessed != null && guessed.first != null) { - charset = guessed.first; + + Charset hardCodedCharset = guessed == null ? null : guessed.first; + if (charsetName != null) { + charset = CharsetToolkit.forName(charsetName); + } + else if (hardCodedCharset == null) { + Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); + if (specifiedExplicitly != null) { + charset = specifiedExplicitly; + } } else { - String charsetName = fileType.getCharset(virtualFile, content); - - if (charsetName == null) { - Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true); - if (specifiedExplicitly != null) { - charset = specifiedExplicitly; - } - } - else { - charset = CharsetToolkit.forName(charsetName); - } + charset = hardCodedCharset; } if (charset == null) { charset = EncodingRegistry.getInstance().getDefaultCharset(); } - if (fileType.getName().equals("Properties") && EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) { - charset = Native2AsciiCharset.wrap(charset); - } virtualFile.setCharset(charset); return charset; } diff --git a/plugins/properties/properties-psi-api/properties-psi-api.iml b/plugins/properties/properties-psi-api/properties-psi-api.iml index bfbb8149469f..fcf7b20750bd 100644 --- a/plugins/properties/properties-psi-api/properties-psi-api.iml +++ b/plugins/properties/properties-psi-api/properties-psi-api.iml @@ -17,5 +17,6 @@ + \ No newline at end of file diff --git a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java index d9b57434d57c..5b82615532c9 100644 --- a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java +++ b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesFileType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,10 @@ package com.intellij.lang.properties; import com.intellij.icons.AllIcons; +import com.intellij.lang.properties.charset.Native2AsciiCharset; +import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.fileTypes.LanguageFileType; +import com.intellij.openapi.util.Trinity; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.encoding.EncodingRegistry; @@ -63,10 +66,14 @@ public class PropertiesFileType extends LanguageFileType { @Override public String getCharset(@NotNull VirtualFile file, @NotNull final byte[] content) { - Charset charset = EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(file); + Trinity guessed = LoadTextUtil.guessFromContent(file, content, content.length); + Charset charset = guessed == null || guessed.first == null ? EncodingRegistry.getInstance().getDefaultCharsetForPropertiesFiles(file) : guessed.first; if (charset == null) { charset = CharsetToolkit.getDefaultSystemCharset(); } + if (EncodingRegistry.getInstance().isNative2Ascii(file)) { + charset = Native2AsciiCharset.wrap(charset); + } return charset.name(); } } diff --git a/plugins/properties/properties.iml b/plugins/properties/properties.iml index 79207be504b0..212af6cb9114 100644 --- a/plugins/properties/properties.iml +++ b/plugins/properties/properties.iml @@ -6,7 +6,6 @@ - @@ -22,5 +21,4 @@ - - + \ No newline at end of file diff --git a/xml/xml-psi-impl/src/com/intellij/ide/highlighter/HtmlFileType.java b/xml/xml-psi-impl/src/com/intellij/ide/highlighter/HtmlFileType.java index 53ee4a387bb7..0f249a096bb5 100644 --- a/xml/xml-psi-impl/src/com/intellij/ide/highlighter/HtmlFileType.java +++ b/xml/xml-psi-impl/src/com/intellij/ide/highlighter/HtmlFileType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,9 @@ import com.intellij.icons.AllIcons; import com.intellij.ide.IdeBundle; import com.intellij.lang.Language; import com.intellij.lang.html.HTMLLanguage; +import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Trinity; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.text.XmlCharsetDetector; @@ -70,7 +72,12 @@ public class HtmlFileType extends XmlLikeFileType { @Override public String getCharset(@NotNull final VirtualFile file, @NotNull final byte[] content) { - String charset = XmlCharsetDetector.extractXmlEncodingFromProlog(content); + Trinity guessed = LoadTextUtil.guessFromContent(file, content, content.length); + String charset = + guessed != null && guessed.first != null + ? guessed.first.name() + : XmlCharsetDetector.extractXmlEncodingFromProlog(content); + if (charset != null) return charset; @NonNls String strContent; try { diff --git a/xml/xml-psi-impl/src/com/intellij/ide/highlighter/XmlLikeFileType.java b/xml/xml-psi-impl/src/com/intellij/ide/highlighter/XmlLikeFileType.java index 27b8671b31ce..c5143b61e132 100644 --- a/xml/xml-psi-impl/src/com/intellij/ide/highlighter/XmlLikeFileType.java +++ b/xml/xml-psi-impl/src/com/intellij/ide/highlighter/XmlLikeFileType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,10 @@ package com.intellij.ide.highlighter; import com.intellij.lang.Language; +import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Trinity; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.text.XmlCharsetDetector; @@ -32,7 +34,11 @@ public abstract class XmlLikeFileType extends LanguageFileType { } @Override public String getCharset(@NotNull VirtualFile file, @NotNull final byte[] content) { - String charset = XmlCharsetDetector.extractXmlEncodingFromProlog(content); + Trinity guessed = LoadTextUtil.guessFromContent(file, content, content.length); + String charset = + guessed != null && guessed.first != null + ? guessed.first.name() + : XmlCharsetDetector.extractXmlEncodingFromProlog(content); return charset == null ? CharsetToolkit.UTF8 : charset; }