<commit_before>/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.typescript.text;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.util.List;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import com.google.common.collect.ImmutableList;
import com.palantir.typescript.bridge.Bridge;
import com.palantir.typescript.bridge.classifier.Classifier;
import com.palantir.typescript.bridge.language.DefinitionInfo;
import com.palantir.typescript.bridge.language.FileDelta;
import com.palantir.typescript.bridge.language.FileDelta.Delta;
import com.palantir.typescript.bridge.language.LanguageService;

/**
 * The editor for TypeScript files.
 *
 * @author tyleradams
 */
public final class TypeScriptEditor extends TextEditor {

    private final ColorManager colorManager;

    private final Bridge bridge;
    private final Classifier classifier;
    private final LanguageService languageService;

    private OutlinePage contentOutlinePage;
    private MyResourceChangeListener resourceChangeListener;

    public TypeScriptEditor() {
        this.colorManager = new ColorManager();

        this.bridge = new Bridge();
        this.classifier = new Classifier(this.bridge);
        this.languageService = new LanguageService(this.bridge);

        this.setSourceViewerConfiguration(new SourceViewerConfiguration(this));

        this.resourceChangeListener = new MyResourceChangeListener();
        ResourcesPlugin.getWorkspace().addResourceChangeListener(this.resourceChangeListener);
    }

    @Override
    public Object getAdapter(Class adapter) {
        if (IContentOutlinePage.class.equals(adapter)) {
            if (this.contentOutlinePage == null) {
                this.contentOutlinePage = new OutlinePage(this);
            }

            return this.contentOutlinePage;
        }

        return super.getAdapter(adapter);
    }

    public Classifier getClassifier() {
        return this.classifier;
    }

    public ColorManager getColorManager() {
        return this.colorManager;
    }

    public LanguageService getLanguageService() {
        return this.languageService;
    }

    @Override
    public void dispose() {
        this.bridge.dispose();
        this.colorManager.dispose();

        ResourcesPlugin.getWorkspace().removeResourceChangeListener(this.resourceChangeListener);
        this.resourceChangeListener = null;

        super.dispose();
    }

    @Override
    protected void createActions() {
        super.createActions();

        // format
        FormatAction formatAction = new FormatAction();
        formatAction.setActionDefinitionId(ITypeScriptActionDefinitionIds.FORMAT);
        this.setAction(ITypeScriptActionDefinitionIds.FORMAT, formatAction);

        // open definition
        OpenDefinitionAction openDefinitionAction = new OpenDefinitionAction();<commit_msg>Share the bridge with all TypeScript editors (for better performance)<commit_after>/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.typescript.text;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.util.List;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.palantir.typescript.bridge.Bridge;
import com.palantir.typescript.bridge.classifier.Classifier;
import com.palantir.typescript.bridge.language.DefinitionInfo;
import com.palantir.typescript.bridge.language.FileDelta;
import com.palantir.typescript.bridge.language.FileDelta.Delta;
import com.palantir.typescript.bridge.language.LanguageService;

/**
 * The editor for TypeScript files.
 *
 * @author tyleradams
 */
public final class TypeScriptEditor extends TextEditor {

    private static final Supplier<Bridge> BRIDGE_SUPPLIER = Suppliers.memoize(new Supplier<Bridge>() {
        @Override
        public Bridge get() {
            return new Bridge();
        }
    });

    private final ColorManager colorManager;

    private final Bridge bridge;
    private final Classifier classifier;
    private final LanguageService languageService;

    private OutlinePage contentOutlinePage;
    private MyResourceChangeListener resourceChangeListener;

    public TypeScriptEditor() {
        this.colorManager = new ColorManager();

        this.bridge = BRIDGE_SUPPLIER.get();
        this.classifier = new Classifier(this.bridge);
        this.languageService = new LanguageService(this.bridge);

        this.setSourceViewerConfiguration(new SourceViewerConfiguration(this));

        this.resourceChangeListener = new MyResourceChangeListener();
        ResourcesPlugin.getWorkspace().addResourceChangeListener(this.resourceChangeListener);
    }

    @Override
    public Object getAdapter(Class adapter) {
        if (IContentOutlinePage.class.equals(adapter)) {
            if (this.contentOutlinePage == null) {
                this.contentOutlinePage = new OutlinePage(this);
            }

            return this.contentOutlinePage;
        }

        return super.getAdapter(adapter);
    }

    public Classifier getClassifier() {
        return this.classifier;
    }

    public ColorManager getColorManager() {
        return this.colorManager;
    }

    public LanguageService getLanguageService() {
        return this.languageService;
    }

    @Override
    public void dispose() {
        this.colorManager.dispose();

        ResourcesPlugin.getWorkspace().removeResourceChangeListener(this.resourceChangeListener);
        this.resourceChangeListener = null;

        super.dispose();
    }

    @Override
    protected void createActions() {
        super.createActions();

        // format
        FormatAction formatAction = new FormatAction();
        formatAction.setActionDefinitionId(ITypeScriptActionDefinitionIds.FORMAT);
        this.setAction(ITypeScriptActionDefinitionIds.FORMAT, formatAction);

        // open definition
        OpenDefinitionAction openDefinitionAction = new OpenDefinitionAction();
{'fraction_non_alphanumeric': 0.08795512277730737, 'fraction_numerical': 0.001693480101608806, 'mean_word_length': 3.9783983140147523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252700', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2508, 'n_words': 570}