<commit_before>/*******************************************************************************
 * Copyright (c) 2014 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package com.codenvy.ide.editor.codemirror.client;

import com.codenvy.ide.editor.codemirror.client.jso.CMEditorOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.CMPixelCoordinatesOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.CMPositionOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.scroll.CMPixelRangeOverlay;
import com.codenvy.ide.jseditor.client.position.PositionConverter;
import com.codenvy.ide.jseditor.client.text.TextPosition;

public class CodemirrorPositionConverter implements PositionConverter {

    private final CMEditorOverlay editorOverlay;

    public CodemirrorPositionConverter(final CMEditorOverlay editorOverlay) {
        this.editorOverlay = editorOverlay;
    }

    public PixelCoordinates textToPixel(final TextPosition textPosition) {
        final CMPositionOverlay position = CMPositionOverlay.create(textPosition.getLine(),
                                                                    textPosition.getCharacter());
        return textToPixel(position);
    }

    private PixelCoordinates textToPixel(final CMPositionOverlay position) {
        final CMPixelRangeOverlay pixelPosition = this.editorOverlay.charCoords(position, "local");
        return new PixelCoordinates(pixelPosition.getLeft(),
                                    pixelPosition.getTop());
    }

    public PixelCoordinates offsetToPixel(final int textOffset) {
        final CMPositionOverlay position = this.editorOverlay.getDoc().posFromIndex(textOffset);
        return textToPixel(position);
    }

    public com.codenvy.ide.jseditor.client.text.TextPosition pixelToText(final PixelCoordinates coordinates) {
        final CMPositionOverlay cmTextPos = pixelToCmText(coordinates);
        return new TextPosition(cmTextPos.getLine(), cmTextPos.getCharacter());
    }

    private CMPositionOverlay pixelToCmText(final PixelCoordinates coordinates) {
        final CMPixelCoordinatesOverlay cmPixel = CMPixelCoordinatesOverlay.create(coordinates.getX(),
                                                                                   coordinates.getY());
        return  this.editorOverlay.coordsChar(cmPixel, "local");
    }

    @Override
    public int pixelToOffset(final PixelCoordinates coordinates) {
        final CMPositionOverlay cmTextPos = pixelToCmText(coordinates);
        return this.editorOverlay.getDoc().indexFromPos(cmTextPos);
    }

}
<commit_msg>IDEX-1601 add 'PositionConverter' to EmbeddedTextEditor interface<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package com.codenvy.ide.editor.codemirror.client;

import com.codenvy.ide.editor.codemirror.client.jso.CMEditorOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.CMPixelCoordinatesOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.CMPositionOverlay;
import com.codenvy.ide.editor.codemirror.client.jso.scroll.CMPixelRangeOverlay;
import com.codenvy.ide.jseditor.client.position.PositionConverter;
import com.codenvy.ide.jseditor.client.text.TextPosition;

public class CodemirrorPositionConverter implements PositionConverter {

    private final CMEditorOverlay editorOverlay;

    public CodemirrorPositionConverter(final CMEditorOverlay editorOverlay) {
        this.editorOverlay = editorOverlay;
    }

    public PixelCoordinates textToPixel(final TextPosition textPosition) {
        final CMPositionOverlay position = CMPositionOverlay.create(textPosition.getLine(),
                                                                    textPosition.getCharacter());
        return textToPixel(position);
    }

    private PixelCoordinates textToPixel(final CMPositionOverlay position) {
        final CMPixelRangeOverlay pixelPosition = this.editorOverlay.charCoords(position, "window");
        return new PixelCoordinates(pixelPosition.getLeft(),
                                    pixelPosition.getTop());
    }

    public PixelCoordinates offsetToPixel(final int textOffset) {
        final CMPositionOverlay position = this.editorOverlay.getDoc().posFromIndex(textOffset);
        return textToPixel(position);
    }

    public com.codenvy.ide.jseditor.client.text.TextPosition pixelToText(final PixelCoordinates coordinates) {
        final CMPositionOverlay cmTextPos = pixelToCmText(coordinates);
        return new TextPosition(cmTextPos.getLine(), cmTextPos.getCharacter());
    }

    private CMPositionOverlay pixelToCmText(final PixelCoordinates coordinates) {
        final CMPixelCoordinatesOverlay cmPixel = CMPixelCoordinatesOverlay.create(coordinates.getX(),
                                                                                   coordinates.getY());
        return  this.editorOverlay.coordsChar(cmPixel, "window");
    }

    @Override
    public int pixelToOffset(final PixelCoordinates coordinates) {
        final CMPositionOverlay cmTextPos = pixelToCmText(coordinates);
        return this.editorOverlay.getDoc().indexFromPos(cmTextPos);
    }

}

{'fraction_non_alphanumeric': 0.12789915966386556, 'fraction_numerical': 0.0033613445378151263, 'mean_word_length': 3.5882806476484195, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774273', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1515, 'n_words': 296}