⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jslinebreaker.java

📁 java写的多功能文件编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                isSpecialChar = true;                continue;            }            // handle comments            if ( !isInQuote && !(isInComment || isLineComment) && line.regionMatches(false, i, "//", 0, 2) )            {                isLineComment = true;                outBuffer.append("//");                i++;                continue;            }            else if ( !isInQuote && !(isInComment || isLineComment) && line.regionMatches(false, i, "/*", 0, 2) )            {                isInComment = true;                outBuffer.append("/*");                i++;                continue;            }            else if ( !isInQuote && (isInComment || isLineComment) && line.regionMatches(false, i, "*/", 0, 2) )            {                isInComment = false;                outBuffer.append("*/");                i++;                continue;            }            if (isInComment || isLineComment)            {                outBuffer.append(ch);                continue;            }            // handle quotes (such as 'x' and "Hello Dolly")            if (ch=='"' || ch=='\'')                if (!isInQuote)                {                    quoteChar = ch;                    isInQuote = true;                }                else if (quoteChar == ch)                {                    isInQuote = false;                    outBuffer.append(ch);                    continue;                }            if (isInQuote)            {                outBuffer.append(ch);                continue;            }            outBuffer.append(ch);            for (int p=0; p<prefs.length; p++)            {                String key = (String) prefs[p];                if (line.regionMatches(false, i, key, 0, key.length()))                {                    int breakType = AFTER;                    if (ch == '(' || ch == '[' || ch == ')' || ch == ']')                    {                        if ("(".equals(key) /*ch == '('*/ || ch == '[')                            parenDepth++;                        else if (/*")".equals(key)*/ ch == ')' || ch == ']')                            parenDepth--;                        breakDepth = parenDepth;                        if (ch == ')' || ch == ']' || key.startsWith("()"))                            breakDepth++;                        if (ch == '(' || ch == '[')                            if( (prevCh>='a' &&prevCh<='z')                                    || (prevCh>='A' &&prevCh<='Z')                                    || (prevCh>='0' &&prevCh<='9')                                    || (prevCh=='.'))                                breakType = AFTER;                            else                                breakType = BEFORE;                        else                            breakType = AFTER;                    }                    if (key.length() > 1)                    {                        outBuffer.append(key.substring(1));                        i += key.length() - 1;                    }                    registerLineBreak(lineBreakStack,                            new LineBreak(key, outBuffer.length()+bufferStart, breakDepth, breakType));                    breakDepth = parenDepth;                    break;                }            }            int bufLength = outBuffer.length() + wsBuffer.length() + previousAfterCut.length() + (isCut ? 8 : 0);            LineBreak curBreak = null;            if (bufLength > preferredLineLength && i < line.length() - lineLengthDeviation)            {                while (!lineBreakStack.isEmpty())                {                    curBreak = (LineBreak) lineBreakStack.elementAt(0);                    if (curBreak.breakWhere-bufferStart < 1) // <-----                    {                        curBreak = null;                        lineBreakStack.removeElementAt(0);                    }                    else                        break;                }                if (curBreak != null)                    lineBreakStack.removeElementAt(0);            }            if (curBreak != null) // in future, think of: && line.length()>i+10) (that was used in the past...)            {                int cutWhere = curBreak.breakWhere - bufferStart - (curBreak.breakType == BEFORE ? curBreak.breakStr.length() : 0);                if (cutWhere < 8)                    continue;                StringBuffer brokenLineBuffer = new StringBuffer();                String outString = outBuffer.toString();                String beforeCut = outString.substring(0, cutWhere);                //brokenLineBuffer.append(wsBuffer);                /*                if (isCut)                    brokenLineBuffer.append("        ");                brokenLineBuffer.append(beforeCut);                brokenLineVector.addElement(brokenLineBuffer.toString());                */                brokenLineBuffer.append(beforeCut);                addBrokenLine(wsBuffer.toString(), brokenLineBuffer.toString(), curBreak, breakDepth, isCut);                //previousAfterCut = outString.substring(cutWhere);                bufferStart += cutWhere;                outBuffer = new StringBuffer(outString.substring(cutWhere));                //lineBreakStack = new Stack();                isCut = true;            }        }        // at end of line:        StringBuffer brokenLineBuffer = new StringBuffer();        //brokenLineBuffer.append(wsBuffer);        /*        if (isCut)            brokenLineBuffer.append("        ");        brokenLineBuffer.append(outBuffer);        brokenLineVector.addElement(brokenLineBuffer.toString());        */        brokenLineBuffer.append(outBuffer);        addBrokenLine(wsBuffer.toString(), brokenLineBuffer.toString(), null, breakDepth, isCut);    }    private void registerLineBreak(Stack lineBreakStack, LineBreak newBreak)    {        LineBreak lastBreak;        while (!lineBreakStack.isEmpty())        {            lastBreak = (LineBreak) lineBreakStack.peek();            if (compare(lastBreak, newBreak) < 0)                lineBreakStack.pop();            else                break;        }        lineBreakStack.push(newBreak);        //newBreak.dump(); /*       System.out.println("for " + newBreak.breakStr);        for (int s=0; s<lineBreakStack.size(); s++)            ((LineBreak) lineBreakStack.elementAt(s)).dump();        System.out.println();  */    }    private LineBreak previousLineBreak = null;    private void addBrokenLine(String whiteSpace, String brokenLine, LineBreak lineBreak, int breakDepth, boolean isCut)    {        boolean isLineAppended = false;        brokenLine = brokenLine.trim();        if (previousLineBreak != null) //&& brokenLine.length() > 0)        {            String previousBrokenLine = (String) brokenLineVector.lastElement();            if (brokenLine.length() + previousBrokenLine.length() <= preferredLineLength + lineLengthDeviation                    || brokenLine.startsWith("{"))            {                if (lineBreak == null ||                        (isNestedConnection && !",".equals(previousLineBreak.breakStr)) ||                        (lineBreak.breakDepth < previousLineBreak.breakDepth) || //dfds                        (lineBreak.breakDepth == previousLineBreak.breakDepth &&                        ((!isNestedConnection && !",".equals(previousLineBreak.breakStr)) ||                             ",".equals(lineBreak.breakStr) || ";".equals(lineBreak.breakStr) ||                             ")".equals(lineBreak.breakStr) || "]".equals(lineBreak.breakStr)))                   )                {                    brokenLineVector.setElementAt( (previousBrokenLine + " " + brokenLine),                            brokenLineVector.size()-1);                    isLineAppended = true;                }            }        }        if (!isLineAppended)        {            if (isCut &&                    !(previousLineBreak != null && ",".equals(previousLineBreak.breakStr) &&                    previousLineBreak.breakDepth == 0))                brokenLine = "        " + brokenLine;            brokenLine = whiteSpace + brokenLine;            brokenLineVector.addElement(brokenLine);        }        previousLineBreak = lineBreak;    }    private int compare(LineBreak br1, LineBreak br2)    {        if (br1.breakDepth < br2.breakDepth)                return 1;            else if (br1.breakDepth > br2.breakDepth)                return -1;        int ord1 = ((Integer) prefTable.get(br1.breakStr)).intValue();        int ord2 = ((Integer) prefTable.get(br2.breakStr)).intValue();        if (ord1 < ord2)            return 1;        else            return -1;    }    boolean hasMoreBrokenLines()    {        return brokenLineVector.size() > 0;    }    String nextBrokenLine()    {        String nextLine;        if (hasMoreBrokenLines())        {            nextLine = (String) brokenLineVector.firstElement();            brokenLineVector.removeElementAt(0);        }        else            return nextLine = "";        return nextLine;    }    class LineBreak    {        String breakStr;        int breakWhere;        int breakDepth;        int breakType;        LineBreak(String str, int wh, int dp, int tp)        {            breakStr = str;            breakWhere = wh;            breakDepth = dp;            breakType = tp;        }        void dump()        {            System.out.println("LB: str="+breakStr+" wh="+breakWhere+" dep="+breakDepth+" tp="+breakType);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -