Search in sources :

Example 1 with Test

use of name.graf.emanuel.testfileeditor.model.node.Test in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class VirtualLineNumberRuler method update.

@Override
public void update(final Observable o, final Object arg) {
    if (o instanceof TestFile) {
        final List<Integer> startLineNumbers = new ArrayList<>();
        final List<Integer> endLineNumbers = new ArrayList<>();
        final TestFile file = (TestFile) o;
        final IDocument document = getEditor().getDocumentProvider().getDocument(getEditor().getEditorInput());
        final Test[] tests = file.getTests();
        try {
            for (final Test test : tests) {
                final Node[] files = test.getChildren();
                Position nodePosition = test.getPosition();
                int realLine = document.getLineOfOffset(nodePosition.offset);
                endLineNumbers.add(realLine + 1);
                for (final Node testNode : files) {
                    nodePosition = testNode.getPosition();
                    realLine = document.getLineOfOffset(nodePosition.offset) + 1;
                    if (testNode instanceof File) {
                        final String name = testNode.toString();
                        if (name.endsWith(".cpp") || name.endsWith(".h") || name.endsWith(".hpp")) {
                            startLineNumbers.add(realLine);
                        } else {
                            endLineNumbers.add(realLine);
                        }
                    } else if (testNode instanceof Expected) {
                        startLineNumbers.add(realLine);
                    } else {
                        endLineNumbers.add(realLine);
                    }
                }
            }
        } catch (final BadLocationException e) {
            e.printStackTrace();
        }
        modelLineToRulerLineMap.clear();
        int largestNumber = 0;
        final int lines = document.getNumberOfLines() + 1;
        boolean inFile = false;
        int rulerLine = 1;
        for (int line = 1; line < lines; ++line) {
            if (startLineNumbers.contains(line)) {
                inFile = true;
                rulerLine = 1;
                continue;
            } else if (endLineNumbers.contains(line)) {
                inFile = false;
            } else if (inFile) {
                if (rulerLine > largestNumber) {
                    largestNumber = rulerLine;
                }
                modelLineToRulerLineMap.put(line - 1, rulerLine++);
            }
        }
        maxDigits = Integer.toString(largestNumber).length();
        redraw();
    }
}
Also used : Expected(name.graf.emanuel.testfileeditor.model.node.Expected) Position(org.eclipse.jface.text.Position) Node(name.graf.emanuel.testfileeditor.model.node.Node) ArrayList(java.util.ArrayList) Test(name.graf.emanuel.testfileeditor.model.node.Test) TestFile(name.graf.emanuel.testfileeditor.model.TestFile) TestFile(name.graf.emanuel.testfileeditor.model.TestFile) File(name.graf.emanuel.testfileeditor.model.node.File) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with Test

use of name.graf.emanuel.testfileeditor.model.node.Test in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class JumpToRTSHandler method jump.

private void jump() {
    if (className != null) {
        IType cls = null;
        try {
            cls = project.findType(className);
        } catch (JavaModelException e) {
            return;
        }
        try {
            IFile file = project.getProject().getFile(getTestFileName(cls));
            IWorkbench workbench = PlatformUI.getWorkbench();
            IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
            FileEditorInput input = new FileEditorInput(file);
            IDocumentProvider provider = new TextFileDocumentProvider();
            provider.connect(input);
            IDocument document = provider.getDocument(input);
            TestFile testFile = new TestFile(input, provider);
            testFile.parse();
            IEditorDescriptor defaultEditor = workbench.getEditorRegistry().getDefaultEditor(file.getName());
            String editorId = defaultEditor.getId();
            if (!className.equals(testName)) {
                for (Test test : testFile.getTests()) {
                    if (test.toString().equals(testName)) {
                        int line = document.getLineOfOffset(test.getPosition().getOffset());
                        IMarker lineMarker = file.createMarker(IMarker.TEXT);
                        lineMarker.setAttribute(IMarker.LINE_NUMBER, line + 1);
                        lineMarker.setAttribute(IDE.EDITOR_ID_ATTR, editorId);
                        IDE.openEditor(page, lineMarker);
                        lineMarker.delete();
                        return;
                    }
                }
            } else {
                IDE.openEditor(page, file);
            }
        } catch (CoreException | BadLocationException | NullPointerException e) {
            e.printStackTrace();
            MessageDialog.openError(shell, "Jump to RTS", "Failed to find associated RTS file.");
        }
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) TextFileDocumentProvider(org.eclipse.ui.editors.text.TextFileDocumentProvider) IType(org.eclipse.jdt.core.IType) IWorkbench(org.eclipse.ui.IWorkbench) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) Test(name.graf.emanuel.testfileeditor.model.node.Test) FileEditorInput(org.eclipse.ui.part.FileEditorInput) TestFile(name.graf.emanuel.testfileeditor.model.TestFile) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IMarker(org.eclipse.core.resources.IMarker) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with Test

use of name.graf.emanuel.testfileeditor.model.node.Test in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class TestFile method addLineNoToMarkerList.

/**
 * @author tstauber
 *
 *         Appends the passed relativeLineNo to the markerLines line in the
 *         .config file associated with the <code>Test</code> which contains
 *         the absolute lineNo passed. If no <code>Test</code> or markerList
 *         could be found the function will not change the file and returns
 *         -1.
 *
 * @param int
 *        lineNo Absolute line number in the file
 * @param int
 *        relativeLineNo Relative line number to the virtual file
 * @return int The length difference from before to after.
 */
public int addLineNoToMarkerList(final int lineNo, final int relativeLineNo) {
    final IDocument document = fProvider.getDocument(fInput);
    int deltaLength = 0;
    try {
        final int offset = document.getLineOffset(lineNo);
        final Test test = getTest(offset);
        if (test == null) {
            return deltaLength;
        }
        deltaLength += addConfigFileIfNotExists(test);
        deltaLength += addMarkerLinePropertyIfNotExists(test);
        deltaLength += addVirtLineNumToMarkerLines(test, relativeLineNo);
    } catch (final IllegalStateException | BadLocationException e) {
    } finally {
        parse();
    }
    return deltaLength;
}
Also used : Test(name.graf.emanuel.testfileeditor.model.node.Test) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 4 with Test

use of name.graf.emanuel.testfileeditor.model.node.Test in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class TestFile method doParse.

private void doParse() throws BadLocationException, BadPositionCategoryException, CoreException {
    final IDocument document = fProvider.getDocument(fInput);
    final int NOF_LINES = document.getNumberOfLines();
    fTests.clear();
    fProblems.clear();
    cleanMarkers();
    Test currentTest = null;
    Test previousTest = null;
    File currentFile = null;
    File previousFile = null;
    ParseState currentState = ParseState.INIT;
    int currentSelectionStart = 0;
    for (int currentLine = 0; currentLine < NOF_LINES; ++currentLine) {
        final int lineOffset = document.getLineOffset(currentLine);
        final int lineLength = document.getLineLength(currentLine);
        final String lineContent = document.get(lineOffset, lineLength);
        if (lineContent.length() > 3) {
            switch(lineContent.substring(0, Tokens.TOKENLENGTH)) {
                case Tokens.TEST:
                    final Position headPos_TEST = new Position(lineOffset, lineLength);
                    final Position pos_TEST = new Position(lineOffset, document.getLength() - lineOffset);
                    document.addPosition(PARTITION_TEST_NAME, headPos_TEST);
                    currentTest = new Test(lineContent.trim().substring(Tokens.TOKENLENGTH), pos_TEST, headPos_TEST, this);
                    if (previousTest != null) {
                        previousTest.getPosition().setLength(lineOffset - previousTest.getPosition().getOffset());
                    }
                    previousTest = currentTest;
                    if (fTests.contains(currentTest)) {
                        final Problem duplicateTest = new DuplicateTest(currentTest.toString(), currentLine + 1, headPos_TEST);
                        reportProblem(duplicateTest);
                        fProblems.add(new DuplicateTest(currentTest.toString(), currentLine + 1, headPos_TEST));
                    } else {
                        fTests.add(currentTest);
                    }
                    currentState = ParseState.TEST;
                    break;
                case Tokens.LANGUAGE:
                    if (previousFile != null) {
                        previousFile.getPosition().setLength(lineOffset - previousFile.getPosition().getOffset());
                        previousFile = null;
                    }
                    if (currentTest != null) {
                        final Position pos_LANG = new Position(lineOffset, lineLength);
                        document.addPosition(PARTITION_TEST_LANGUAGE, pos_LANG);
                        currentTest.setLang(new Language(lineContent.trim().substring(Tokens.TOKENLENGTH), pos_LANG, currentTest));
                    }
                    break;
                case Tokens.EXPECTED:
                    if (previousFile != null) {
                        previousFile.getPosition().setLength(lineOffset - previousFile.getPosition().getOffset());
                        previousFile = null;
                    }
                    switch(currentState) {
                        case SELECTION:
                            final Position pos_SEL_OPEN = new Position(currentSelectionStart, lineOffset - currentSelectionStart);
                            document.addPosition(PARTITION_TEST_EXPECTED, pos_SEL_OPEN);
                            currentFile.setSelection(new Selection(pos_SEL_OPEN, currentFile));
                            currentState = ParseState.FILE;
                        case FILE:
                            if (currentTest != null) {
                                final Position pos_FILE = new Position(lineOffset, lineLength);
                                document.addPosition(PARTITION_TEST_EXPECTED, pos_FILE);
                                currentTest.setExpected(new Expected(currentTest, lineContent.trim().substring(Tokens.TOKENLENGTH), pos_FILE));
                            }
                            break;
                        default:
                    }
                    break;
                case Tokens.FILE:
                    if (currentTest != null) {
                        if (currentState == ParseState.SELECTION) {
                            final Position pos_SEL_OPEN = new Position(currentSelectionStart, lineOffset - currentSelectionStart);
                            document.addPosition(PARTITION_TEST_SELECTION, pos_SEL_OPEN);
                            currentFile.setSelection(new Selection(pos_SEL_OPEN, currentFile));
                        }
                        final Position headPos_FILE = new Position(lineOffset, lineLength);
                        final Position pos_FILE = new Position(lineOffset, lineLength);
                        document.addPosition(PARTITION_TEST_FILE, headPos_FILE);
                        currentFile = new File(lineContent.trim().substring(Tokens.TOKENLENGTH), pos_FILE, headPos_FILE, currentTest);
                        if (previousFile != null) {
                            previousFile.getPosition().setLength(lineOffset - previousFile.getPosition().getOffset());
                        }
                        previousFile = currentFile;
                        currentTest.addFile(currentFile);
                        currentState = ParseState.FILE;
                    }
                    break;
                case Tokens.CLASS:
                    if (previousFile != null) {
                        previousFile.getPosition().setLength(lineOffset - previousFile.getPosition().getOffset());
                        previousFile = null;
                    }
                    if (currentState == ParseState.TEST) {
                        final Position pos_CLASS = new Position(lineOffset, lineLength);
                        document.addPosition(PARTITION_TEST_CLASS, pos_CLASS);
                        currentTest.setClassname(new Class(lineContent.trim().substring(Tokens.TOKENLENGTH), pos_CLASS, currentTest));
                    }
                    break;
                case Tokens.SELECTION_OPEN:
                    if (currentState == ParseState.FILE) {
                        currentState = ParseState.SELECTION;
                        currentSelectionStart = lineOffset + lineContent.indexOf(Tokens.SELECTION_OPEN);
                    }
                    break;
            }
        }
        if (lineContent.contains(Tokens.SELECTION_CLOSE) && currentState == ParseState.SELECTION) {
            final Position pos_SEL_CLOSE = new Position(currentSelectionStart, lineOffset + lineContent.indexOf(Tokens.SELECTION_CLOSE) - currentSelectionStart);
            document.addPosition(PARTITION_TEST_SELECTION, pos_SEL_CLOSE);
            currentFile.setSelection(new Selection(pos_SEL_CLOSE, currentFile));
            currentState = ParseState.FILE;
        }
    }
    setChanged();
}
Also used : Expected(name.graf.emanuel.testfileeditor.model.node.Expected) Position(org.eclipse.jface.text.Position) Selection(name.graf.emanuel.testfileeditor.model.node.Selection) Language(name.graf.emanuel.testfileeditor.model.node.Language) Test(name.graf.emanuel.testfileeditor.model.node.Test) Class(name.graf.emanuel.testfileeditor.model.node.Class) IFile(org.eclipse.core.resources.IFile) File(name.graf.emanuel.testfileeditor.model.node.File) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

Test (name.graf.emanuel.testfileeditor.model.node.Test)4 IDocument (org.eclipse.jface.text.IDocument)4 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TestFile (name.graf.emanuel.testfileeditor.model.TestFile)2 Expected (name.graf.emanuel.testfileeditor.model.node.Expected)2 File (name.graf.emanuel.testfileeditor.model.node.File)2 IFile (org.eclipse.core.resources.IFile)2 Position (org.eclipse.jface.text.Position)2 ArrayList (java.util.ArrayList)1 Class (name.graf.emanuel.testfileeditor.model.node.Class)1 Language (name.graf.emanuel.testfileeditor.model.node.Language)1 Node (name.graf.emanuel.testfileeditor.model.node.Node)1 Selection (name.graf.emanuel.testfileeditor.model.node.Selection)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IEditorDescriptor (org.eclipse.ui.IEditorDescriptor)1 IWorkbench (org.eclipse.ui.IWorkbench)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1