<commit_before>/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * 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
 *
 *******************************************************************************/
package org.eclipse.xtext.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.crossref.IFragmentProvider;
import org.eclipse.xtext.crossref.ILinker;
import org.eclipse.xtext.parser.IAstFactory;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.parsetree.NodeContentAdapter;
import org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor;
import org.eclipse.xtext.parsetree.reconstr.ITokenSerializer;
import org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor.IAbstractToken;
import org.eclipse.xtext.service.Inject;

/**
 * An EMF resource that reads and writes models of an Xtext DSL.
 * 
 * @author <NAME>
 */
public class XtextResource extends ResourceImpl {
	@Inject
	private IParser parser;

	@Inject
	private IAstFactory elementFactory;

	@Inject
	private ILinker linker;

	@Inject
	private IFragmentProvider fragmentProvider;
	
	@Inject
	private IParseTreeConstructor parseTreeConstructor;
	
	@Inject
	private ITokenSerializer tokenSerializer;
	
	private IParseResult parseResult;

	public XtextResource(URI uri) {
		super(uri);
	}

	public IParseResult getParseResult() {
		return parseResult;
	}

	public void update(int offset, int replacedTextLength, String newText) {
		CompositeNode rootNode = parseResult.getRootNode();

		// unloading is required to ensure that any EObjects hanging around
		// (e.g. in the outline) get a proxied URI
		// and thus still can be compared by their URI
		unload();

		parseResult = parser.reparse(rootNode, offset, replacedTextLength, newText);
		clearOutput();
		if (parseResult != null) {
			if (parseResult.getRootASTElement() != null)
				getContents().add(parseResult.getRootASTElement());
			if (parseResult.getRootNode() != rootNode) {
				addNodeContentAdapter();
			}
		}
		doLinking();
	}

	protected void doLinking() {
		if (parseResult.getRootASTElement() == null)
			return;
		indexIds();
		List<Diagnostic> brokenLinks = linker.ensureLinked(parseResult.getRootASTElement());
		TreeIterator<EObject> allContents = parseResult.getRootASTElement().eAllContents();
		while (allContents.hasNext())
			brokenLinks.addAll(linker.ensureLinked(allContents.next()));

		getErrors().addAll(brokenLinks);
		//logger.debug("errors: " + errors.size());
	}

	private void addNodeContentAdapter() {
		parseResult.getRootNode().eAdapters().add(new NodeContentAdapter());
	}

	@Override
	protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
		clearOutput();
		parseResult = parser.parse(inputStream, elementFactory);
		if (parseResult != null) {
			EObject rootElement = parseResult.getRootASTElement();
			if (rootElement != null) {
				getContents().add(rootElement);
			}
			addNodeContentAdapter();
		}
		doLinking();
	}

	private void clearOutput() {<commit_msg>Fixed Bug 252937: Reimplement Reconciling<commit_after>/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * 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
 *
 *******************************************************************************/
package org.eclipse.xtext.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.crossref.IFragmentProvider;
import org.eclipse.xtext.crossref.ILinker;
import org.eclipse.xtext.parser.IAstFactory;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.parsetree.NodeContentAdapter;
import org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor;
import org.eclipse.xtext.parsetree.reconstr.ITokenSerializer;
import org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor.IAbstractToken;
import org.eclipse.xtext.service.Inject;
import org.eclipse.xtext.util.StringInputStream;

/**
 * An EMF resource that reads and writes models of an Xtext DSL.
 * 
 * @author <NAME>
 */
public class XtextResource extends ResourceImpl {
	@Inject
	private IParser parser;

	@Inject
	private IAstFactory elementFactory;

	@Inject
	private ILinker linker;

	@Inject
	private IFragmentProvider fragmentProvider;
	
	@Inject
	private IParseTreeConstructor parseTreeConstructor;
	
	@Inject
	private ITokenSerializer tokenSerializer;
	
	private IParseResult parseResult;

	public XtextResource(URI uri) {
		super(uri);
	}

	public IParseResult getParseResult() {
		return parseResult;
	}

	public void reparse(String newContent) {
		doLoad(new StringInputStream(newContent), null);
	}
	
	public void update(int offset, int replacedTextLength, String newText) {
		CompositeNode rootNode = parseResult.getRootNode();

		// unloading is required to ensure that any EObjects hanging around
		// (e.g. in the outline) get a proxied URI
		// and thus still can be compared by their URI
		unload();

		parseResult = parser.reparse(rootNode, offset, replacedTextLength, newText);
		clearOutput();
		if (parseResult != null) {
			if (parseResult.getRootASTElement() != null)
				getContents().add(parseResult.getRootASTElement());
			if (parseResult.getRootNode() != rootNode) {
				addNodeContentAdapter();
			}
		}
		doLinking();
	}

	protected void doLinking() {
		if (parseResult.getRootASTElement() == null)
			return;
		indexIds();
		List<Diagnostic> brokenLinks = linker.ensureLinked(parseResult.getRootASTElement());
		TreeIterator<EObject> allContents = parseResult.getRootASTElement().eAllContents();
		while (allContents.hasNext())
			brokenLinks.addAll(linker.ensureLinked(allContents.next()));

		getErrors().addAll(brokenLinks);
		//logger.debug("errors: " + errors.size());
	}

	private void addNodeContentAdapter() {
		parseResult.getRootNode().eAdapters().add(new NodeContentAdapter());
	}

	@Override
	protected void doLoad(InputStream inputStream, Map<?, ?> options) {
		clearOutput();
		parseResult = parser.parse(inputStream, elementFactory);
		if (parseResult != null) {
			EObject rootElement = parseResult.getRootASTElement();
			if (rootElement != null) {
				getContents().add(rootElement);
			}
			addNodeContentAdapter();
		}
		doLinking();
	}

	private void clearOutput() {
{'fraction_non_alphanumeric': 0.14606155451225875, 'fraction_numerical': 0.0028690662493479394, 'mean_word_length': 5.296387520525451, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3875035', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2241, 'n_words': 515}