PATCH-1559330: PHPDoc comments are garbled in function hover. Applied patch and refac...
authorpombredanne <pombredanne>
Fri, 2 Mar 2007 23:24:52 +0000 (23:24 +0000)
committerpombredanne <pombredanne>
Fri, 2 Mar 2007 23:24:52 +0000 (23:24 +0000)
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/phpdoc/PHPDocUtil.java

index 0d477e1..9b30f8b 100644 (file)
@@ -1,10 +1,16 @@
 package net.sourceforge.phpdt.internal.corext.phpdoc;
 
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
 
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
 
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+
 /**
  * Utility class for static PHPdoc helper mehods
  */
@@ -19,7 +25,6 @@ public class PHPDocUtil {
         */
        public static void appendPHPDoc(StringBuffer hoverInfoBuffer,
                        String filename, PHPIdentifierLocation location) {
-               FileReader phpFileReader;
                hoverInfoBuffer.append(location.toString());
                hoverInfoBuffer.append(" - <b>");
                try {
@@ -28,7 +33,9 @@ public class PHPDocUtil {
 
                        // read the phpdoc for the function
                        if (location.getPHPDocOffset() >= 0) {
-                               phpFileReader = new FileReader(filename);
+                               String encoding = getEncoding(filename);
+                               InputStreamReader phpFileReader = new InputStreamReader(
+                                               new FileInputStream(filename), encoding);
                                char[] phpDocDeclarationCharArray = new char[location
                                                .getPHPDocLength()];
                                phpFileReader.skip(location.getPHPDocOffset());
@@ -37,18 +44,31 @@ public class PHPDocUtil {
                                PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(
                                                phpDocDeclarationCharArray);
                                hoverInfoBuffer.append(phpdocConverter.getString());
-                               // hoverInfoBuffer.append("<br><br>");
                                phpFileReader.close();
                        }
 
                } catch (IOException e) {
+                       // TODO: smell
                        return;
                }
        }
 
+       static String getEncoding(String filename) {
+               String encoding = null;
+               IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
+                               .getFileForLocation(new Path(filename));
+               if (file != null) {
+                       try {
+                               encoding = file.getCharset();
+                       } catch (CoreException e) {
+                               // TODO: should log the fact that we could not get the encoding?
+                       }
+               }
+               return encoding;
+       }
+
        public static String getUsage(String filename,
                        PHPIdentifierLocation location) {
-               FileReader phpFileReader;
                String usage = location.getUsage();
                if (usage != null) {
                        return usage;
@@ -56,7 +76,9 @@ public class PHPDocUtil {
                usage = "";
                try {
 
-                       phpFileReader = new FileReader(filename);
+                       String encoding = getEncoding(filename);
+                       InputStreamReader phpFileReader = new InputStreamReader(
+                                       new FileInputStream(filename), encoding);
                        // read the function declaration
                        if (location.getOffset() >= 0
                                        && (location.isMethod() || location.isConstructor()