<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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 de.jcup.egradle.eclipse.gradleeditor.document;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.WordPatternRule;

public class ExactWordPatternRule extends WordPatternRule{

	private String toStringValue;
	StringBuilder traceSb;
	boolean trace = false;
	
	public ExactWordPatternRule(IWordDetector detector, String exactWord, IToken token) {
		super(detector, exactWord, null, token);
		toStringValue=getClass().getSimpleName()+":"+exactWord;
	}
	
	protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) {
		/* sequence is not the word found by word detector but the start sequence!!!!! (in this case always the exact word)*/
		
		// -------------------------------------------------
		// example: exactWord='test' 
		// 
		// subjects: atest,test,testa
		//                   ^----------------only result!
		Counter counter = new Counter();
		if (trace){
			// trace contains NOT first character, this is done at PatternRule
			traceSb = new StringBuilder();
		}
		int column=scanner.getColumn();
		boolean wordHasPrefix;
		if (column==1){
			wordHasPrefix=false;
		}else{
			scannerUnread(scanner, counter);
			scannerUnread(scanner, counter);
			char charBefore =(char)scannerRead(scanner, counter);
			scannerRead(scanner, counter);
			wordHasPrefix =fDetector.isWordPart(charBefore);
		}
		if (wordHasPrefix){
			scannerRead(scanner, counter);
			return counter.cleanupAndReturn(scanner,false);
		}
		for (int i= 1; i < sequence.length; i++) {
			int c= scannerRead(scanner, counter);
			if (c == ICharacterScanner.EOF){
				if (eofAllowed) {
					return counter.cleanupAndReturn(scanner,true);
				}else{
					return counter.cleanupAndReturn(scanner,false);
				}
			} else if (c != sequence[i]) {
				scannerUnread(scanner, counter);
				for (int j= i-1; j > 0; j--){
					scannerUnread(scanner, counter);
				}
				return counter.cleanupAndReturn(scanner,false);
			}
		}
		char charAfter = (char)scannerRead(scanner, counter);
		scannerUnread(scanner, counter);
		
		if (fDetector.isWordPart(charAfter)){
			/* the word is more than the exact one - e.g. instead of 'test' 'testx' ... so not correct*/
			return counter.cleanupAndReturn(scanner,false);
		}
		return counter.cleanupAndReturn(scanner,true);
	}

	
	private int scannerRead(ICharacterScanner scanner, Counter counter) {
		int c = scanner.read();
		if (c!=ICharacterScanner.EOF){
			counter.count++;
			if (trace){
				traceSb.append((char)c);
			}
		}<commit_msg>Fixed exact word pattern rule like in bashEditor<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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 de.jcup.egradle.eclipse.gradleeditor.document;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.WordPatternRule;

public class ExactWordPatternRule extends WordPatternRule{

	private String toStringValue;
	StringBuilder traceSb;
	boolean trace = false;
	
	public ExactWordPatternRule(IWordDetector detector, String exactWord, IToken token) {
		super(detector, exactWord, null, token);
		toStringValue=getClass().getSimpleName()+":"+exactWord;
	}
	
	protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) {
		/* sequence is not the word found by word detector but the start sequence!!!!! (in this case always the exact word)*/
		
		// -------------------------------------------------
		// example: exactWord='test' 
		// 
		// subjects: atest,test,testa
		//                   ^----------------only result!
		Counter counter = new Counter();
		if (trace){
			// trace contains NOT first character, this is done at PatternRule
			traceSb = new StringBuilder();
		}
		int column=scanner.getColumn();
		boolean wordHasPrefix;
		if (column==1){
			wordHasPrefix=false;
		}else{
			scannerUnread(scanner, counter);
			scannerUnread(scanner, counter);
			char charBefore =(char)scannerRead(scanner, counter);
			scannerRead(scanner, counter);
			wordHasPrefix =fDetector.isWordPart(charBefore);
		}
		if (wordHasPrefix){
			scannerRead(scanner, counter);
			return counter.cleanupAndReturn(scanner,false);
		}
		for (int i= 1; i < sequence.length; i++) {
			int c= scannerRead(scanner, counter);
			if (c == ICharacterScanner.EOF){
				if (eofAllowed) {
					return counter.cleanupAndReturn(scanner,true);
				}else{
					return counter.cleanupAndReturn(scanner,false);
				}
			} else if (c != sequence[i]) {
				scannerUnread(scanner, counter);
				for (int j= i-1; j > 0; j--){
					scannerUnread(scanner, counter);
				}
				return counter.cleanupAndReturn(scanner,false);
			}
		}
		char charAfter = (char)scannerRead(scanner, counter);
		scannerUnread(scanner, counter);
		
		/* when not a whitespace and not end reached - do cleanup*/
		if (! Character.isWhitespace(charAfter) && ICharacterScanner.EOF!=charAfter){
			/* the word is more than the exact one - e.g. instead of 'test' 'testx' ... so not correct*/
			return counter.cleanupAndReturn(scanner,false);
		}
		return counter.cleanupAndReturn(scanner,true);
	}

	
	private int scannerRead(ICharacterScanner scanner, Counter counter) {
		int c = scanner.read();
		if (c!=ICharacterScanner.EOF){
			counter.count++;
			if (trace){
				traceSb.append((char)c);
			}
		}
{'fraction_non_alphanumeric': 0.1288120163859809, 'fraction_numerical': 0.0036413290851160674, 'mean_word_length': 3.652081863091037, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '24137', 'n_tokens_mistral': 2221, 'n_tokens_neox': 1945, 'n_words': 572}