int lastIndexInLine = lineAsRegion.getOffset() + lineAsRegion.getLength() -1; char c; int stateOfScanner = 0; int leftmostIndex = 0; int rightmostIndex = 0; boolean directiveFound = false; for(int i = lineAsRegion.getOffset(); i <= lastIndexInLine;i++) { try { c = document.getChar(i); if(stateOfScanner == 0) { if(Character.isWhitespace(c)) { continue; } stateOfScanner = 1; } if(stateOfScanner == 1) { if(c == '/') { stateOfScanner = 2; continue; } } if(stateOfScanner == 2) { if(c == '/') { stateOfScanner = 3; continue; } } if(stateOfScanner == 3) { if(c == '#') { stateOfScanner = 4; continue; } } if(stateOfScanner == 4) { directiveFound = true; leftmostIndex = i; rightmostIndex = i; if(Character.isJavaIdentifierPart(c)) { stateOfScanner = 5; continue; }  break; } if(stateOfScanner == 5) { if(Character.isJavaIdentifierPart(c)) { rightmostIndex = i; continue; }  rightmostIndex--; break; } } catch(BadLocationException exception) { System.out.println("ERROR:PolishDocumentUtils.getDirectiveFromLine(...):wrong offset of char:"+exception); return null;
String lineAsString; try { lineAsString = document.get(lineAsRegion.getOffset(),lineAsRegion.getLength()); } catch (BadLocationException exception) {  exception.printStackTrace(); return null; }  int indexOf = lineAsString.indexOf(" if(indexOf == -1) {  return null; }  int directiveOffset = indexOf+3; int lengthOfLine = lineAsString.length(); if(directiveOffset >= lengthOfLine) { return null; }   if( ! Character.isJavaIdentifierStart(lineAsString.charAt(directiveOffset))) { return null; } int i = 0;    for(i = directiveOffset+1;i<lengthOfLine;i++) { if( ! Character.isJavaIdentifierPart(lineAsString.charAt(i))){ break;
    public static Position extractDirectiveFromLine(IDocument document, int currentLine) {        IRegion lineAsRegion;                try {            lineAsRegion = document.getLineInformation(currentLine);             //lineAsString = document.get(lineAsRegion.getOffset(),lineAsRegion.getLength());        }        catch (BadLocationException exception) {            System.out.println("ERROR:PolishDocumentUtils.getDirectiveFromLine(...):wrong currentLine."+exception);	        return null;        }                int lastIndexInLine = lineAsRegion.getOffset() + lineAsRegion.getLength() -1;        char c;        int stateOfScanner = 0;        int leftmostIndex = 0;        int rightmostIndex = 0;        boolean directiveFound = false;        for(int i = lineAsRegion.getOffset(); i <= lastIndexInLine;i++) {            try {                c = document.getChar(i);                if(stateOfScanner == 0) {                    if(Character.isWhitespace(c)) {                        continue;                    }                    stateOfScanner = 1;                }                if(stateOfScanner == 1) {                    if(c == '/') {                        stateOfScanner = 2;                        continue;                    }                }                if(stateOfScanner == 2) {                    if(c == '/') {                        stateOfScanner = 3;                        continue;                    }                }                if(stateOfScanner == 3) {                    if(c == '#') {                        stateOfScanner = 4;                        continue;                     }                }                if(stateOfScanner == 4) { // First char of directive found.                    directiveFound = true;                    leftmostIndex = i;                    rightmostIndex = i;                    if(Character.isJavaIdentifierPart(c)) {                        stateOfScanner = 5;                        continue;                    }                    //directiveFound = false; //the char after # is not valid. abort.                    break;                }                if(stateOfScanner == 5) {                    if(Character.isJavaIdentifierPart(c)) {                        rightmostIndex = i;                        continue;                    }                    //directiveFound = true; //the char after # is not valid. abort.                    rightmostIndex--; //We went to far by one char.                    break;                }            }            catch(BadLocationException exception) {                System.out.println("ERROR:PolishDocumentUtils.getDirectiveFromLine(...):wrong offset of char:"+exception);    	        return null;            }        }        if(directiveFound) {            return new Position(leftmostIndex,rightmostIndex-leftmostIndex+1);        }        return null;    }