Better language detection
This commit is contained in:
		
							parent
							
								
									54fc4e5e48
								
							
						
					
					
						commit
						347b6acf0f
					
				
					 3 changed files with 22 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -3,13 +3,11 @@ package the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer;
 | 
			
		|||
import java.awt.BorderLayout;
 | 
			
		||||
import java.awt.Font;
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import javax.swing.ImageIcon;
 | 
			
		||||
import javax.swing.JButton;
 | 
			
		||||
import javax.swing.JLabel;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
import org.apache.commons.io.FilenameUtils;
 | 
			
		||||
import org.fife.ui.rsyntaxtextarea.FileTypeUtil;
 | 
			
		||||
import org.imgscalr.Scalr;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.Configuration;
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +19,7 @@ import the.bytecode.club.bytecodeviewer.resources.Resource;
 | 
			
		|||
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.resources.ResourceType;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
 | 
			
		||||
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
 * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite        *
 | 
			
		||||
| 
						 | 
				
			
			@ -144,7 +143,7 @@ public class FileViewer extends ResourceViewer
 | 
			
		|||
        }
 | 
			
		||||
        
 | 
			
		||||
        textArea.setCodeFoldingEnabled(true);
 | 
			
		||||
        textArea.setSyntaxEditingStyle(FileTypeUtil.get().guessContentType(new File(nameLowerCase)));
 | 
			
		||||
        SyntaxLanguage.setLanguage(textArea, nameLowerCase);
 | 
			
		||||
        textArea.setText(contentsAsString);
 | 
			
		||||
        textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, (int) BytecodeViewer.viewer.fontSpinner.getValue()));
 | 
			
		||||
        textArea.setCaretPosition(0);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,6 @@ import javax.swing.JPanel;
 | 
			
		|||
import me.konloch.kontainer.io.DiskReader;
 | 
			
		||||
import me.konloch.kontainer.io.DiskWriter;
 | 
			
		||||
import org.apache.commons.compress.utils.FileNameUtils;
 | 
			
		||||
import org.fife.ui.rsyntaxtextarea.FileTypeUtil;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.Configuration;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
 | 
			
		||||
| 
						 | 
				
			
			@ -27,6 +26,7 @@ import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenu;
 | 
			
		|||
import the.bytecode.club.bytecodeviewer.translation.components.TranslatedJMenuItem;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.util.DialogUtils;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
 | 
			
		||||
import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
 | 
			
		||||
 | 
			
		||||
import static the.bytecode.club.bytecodeviewer.Constants.fs;
 | 
			
		||||
import static the.bytecode.club.bytecodeviewer.Constants.tempDirectory;
 | 
			
		||||
| 
						 | 
				
			
			@ -88,7 +88,7 @@ public class PluginWriter extends JFrame
 | 
			
		|||
		area.setOnCtrlS(this::save);
 | 
			
		||||
		area.setText(content);
 | 
			
		||||
		area.setCaretPosition(0);
 | 
			
		||||
		area.setSyntaxEditingStyle(FileTypeUtil.get().guessContentType(new File(pluginName)));
 | 
			
		||||
		SyntaxLanguage.setLanguage(area, pluginName);
 | 
			
		||||
		content = null;
 | 
			
		||||
		
 | 
			
		||||
		JButton run = new JButton("Run");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,9 @@
 | 
			
		|||
package the.bytecode.club.bytecodeviewer.util;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.util.function.BiFunction;
 | 
			
		||||
import org.fife.ui.rsyntaxtextarea.FileTypeUtil;
 | 
			
		||||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
 | 
			
		||||
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
 | 
			
		||||
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
| 
						 | 
				
			
			@ -23,11 +26,8 @@ import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
 | 
			
		|||
 | 
			
		||||
/**
 | 
			
		||||
 * @author ThexXTURBOXx
 | 
			
		||||
 * @deprecated See {@link org.fife.ui.rsyntaxtextarea.FileTypeUtil#guessContentType(java.io.File)}
 | 
			
		||||
 */
 | 
			
		||||
@Deprecated
 | 
			
		||||
public enum SyntaxLanguage
 | 
			
		||||
{
 | 
			
		||||
public enum SyntaxLanguage {
 | 
			
		||||
    XML(SyntaxConstants.SYNTAX_STYLE_XML,
 | 
			
		||||
            (n, c) -> n.endsWith(".xml") || c.startsWith("<?xml") || c.startsWith("<xml")),
 | 
			
		||||
    PYTHON(SyntaxConstants.SYNTAX_STYLE_PYTHON, (n, c) -> n.endsWith(".py") || n.endsWith(".python")),
 | 
			
		||||
| 
						 | 
				
			
			@ -69,6 +69,8 @@ public enum SyntaxLanguage
 | 
			
		|||
 | 
			
		||||
    public static final SyntaxLanguage[] VALUES = values();
 | 
			
		||||
 | 
			
		||||
    private static final FileTypeUtil FILE_TYPE_UTIL = FileTypeUtil.get();
 | 
			
		||||
 | 
			
		||||
    private final BiFunction<String, String, Boolean> criteria;
 | 
			
		||||
 | 
			
		||||
    private final String syntaxConstant;
 | 
			
		||||
| 
						 | 
				
			
			@ -86,6 +88,10 @@ public enum SyntaxLanguage
 | 
			
		|||
        return syntaxConstant;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated See {@link #setLanguage(String, RSyntaxTextArea)}.
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static SyntaxLanguage detectLanguage(String fileName, String content) {
 | 
			
		||||
        for (SyntaxLanguage lang : VALUES) {
 | 
			
		||||
            if (lang.isLanguage(fileName, content)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -94,4 +100,12 @@ public enum SyntaxLanguage
 | 
			
		|||
        }
 | 
			
		||||
        return NONE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void setLanguage(RSyntaxTextArea area, String fileName) {
 | 
			
		||||
        String type = FILE_TYPE_UTIL.guessContentType(new File(fileName));
 | 
			
		||||
        if (type == null || type.equals(SyntaxConstants.SYNTAX_STYLE_NONE)) {
 | 
			
		||||
            type = FILE_TYPE_UTIL.guessContentType(area);
 | 
			
		||||
        }
 | 
			
		||||
        area.setSyntaxEditingStyle(type);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue