<commit_before>package org.strategoxt.imp.runtime.editor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.strategoxt.imp.runtime.EditorState;
import org.strategoxt.imp.runtime.dynamicloading.BadDescriptorException;
import org.strategoxt.imp.runtime.services.views.properties.IPropertiesService;

/**
 * @author <NAME>
 * 
 * Why implement IStructuredSelection? Because the properties view only takes IStructuredSelections.
 */
public class StrategoTermSelection extends TextSelection implements IStructuredSelection {

	private final SpoofaxEditor spoofaxEditor;

	public StrategoTermSelection(SpoofaxEditor spoofaxEditor, int offset, int length) {
		super(spoofaxEditor.getDocumentProvider().getDocument(spoofaxEditor.getEditorInput()), offset, length);
		this.spoofaxEditor = spoofaxEditor;
	}

	public IStrategoTerm getSelectionAst() {
		if (EditorState.getEditorFor(spoofaxEditor) != null) {
			return SelectionUtil.getSelectionAst(getOffset(), getLength(), false, EditorState.getEditorFor(spoofaxEditor).getParseController());
		}
		return null;
	}
	
	public IStrategoTerm getSelectionAstAnalyzed() {
		if (EditorState.getEditorFor(spoofaxEditor) != null) {
			return SelectionUtil.getSelectionAstAnalyzed(getOffset(), getLength(), false, EditorState.getEditorFor(spoofaxEditor).getParseController());
		}
		return null;
	}
	
	private IStrategoTerm properties;
	
	/**
	 * Return the properties for the properties view.
	 */
	@Override // IStructuredSelection (properties view)
	public IStrategoTerm getFirstElement() {
		if (properties == null) {
			EditorState editorState = EditorState.getEditorFor(spoofaxEditor);
			IPropertiesService propertiesService = null;
			try {
				propertiesService = editorState.getDescriptor().createService(IPropertiesService.class, editorState.getParseController());
			} catch (BadDescriptorException e) {
				e.printStackTrace();
			}
			properties = propertiesService.getProperties(getOffset(), getLength());
		}
		
		return properties;
	}

	@Override // IStructuredSelection (properties view)
	public Iterator<IStrategoTerm> iterator() {
		return toList().iterator();
	}

	@Override // IStructuredSelection (properties view)
	public int size() {
		return toList().size();
	}

	@Override // IStructuredSelection (properties view)
	public Object[] toArray() {
		return toList().toArray();
	}

	@Override // IStructuredSelection (properties view)<commit_msg>bugfix: NPE when opening file with SpoofaxEditor while no language for file extension has been loaded<commit_after>package org.strategoxt.imp.runtime.editor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.strategoxt.imp.runtime.EditorState;
import org.strategoxt.imp.runtime.dynamicloading.BadDescriptorException;
import org.strategoxt.imp.runtime.services.views.properties.IPropertiesService;

/**
 * @author <NAME>
 * 
 * Why implement IStructuredSelection? Because the properties view only takes IStructuredSelections.
 */
public class StrategoTermSelection extends TextSelection implements IStructuredSelection {

	private final SpoofaxEditor spoofaxEditor;

	public StrategoTermSelection(SpoofaxEditor spoofaxEditor, int offset, int length) {
		super(spoofaxEditor.getDocumentProvider().getDocument(spoofaxEditor.getEditorInput()), offset, length);
		this.spoofaxEditor = spoofaxEditor;
	}

	public IStrategoTerm getSelectionAst() {
		if (EditorState.getEditorFor(spoofaxEditor) != null) {
			return SelectionUtil.getSelectionAst(getOffset(), getLength(), false, EditorState.getEditorFor(spoofaxEditor).getParseController());
		}
		return null;
	}
	
	public IStrategoTerm getSelectionAstAnalyzed() {
		if (EditorState.getEditorFor(spoofaxEditor) != null) {
			return SelectionUtil.getSelectionAstAnalyzed(getOffset(), getLength(), false, EditorState.getEditorFor(spoofaxEditor).getParseController());
		}
		return null;
	}
	
	private IStrategoTerm properties;
	
	/**
	 * Return the properties for the properties view.
	 */
	@Override // IStructuredSelection (properties view)
	public IStrategoTerm getFirstElement() {
		if (properties == null) {
			EditorState editorState = EditorState.getEditorFor(spoofaxEditor);
			
			if (editorState == null) {
				return null;
			}
			
			IPropertiesService propertiesService = null;
			try {
				propertiesService = editorState.getDescriptor().createService(IPropertiesService.class, editorState.getParseController());
			} catch (BadDescriptorException e) {
				e.printStackTrace();
			}
			properties = propertiesService.getProperties(getOffset(), getLength());
		}
		
		return properties;
	}

	@Override // IStructuredSelection (properties view)
	public Iterator<IStrategoTerm> iterator() {
		return toList().iterator();
	}

	@Override // IStructuredSelection (properties view)
	public int size() {
		return toList().size();
	}

	@Override // IStructuredSelection (properties view)
	public Object[] toArray() {
		return toList().toArray();
	}

	@Override // IStructuredSelection (properties view)
{'fraction_non_alphanumeric': 0.10236220472440945, 'fraction_numerical': 0.0, 'mean_word_length': 5.619106699751861, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2020609', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1514, 'n_words': 321}