diff options
Diffstat (limited to 'bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team')
5 files changed, 199 insertions, 0 deletions
diff --git a/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitionScanner.java b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitionScanner.java new file mode 100644 index 000000000..b4171bc27 --- /dev/null +++ b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitionScanner.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. 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 + * + * Contributors: + * Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.team.internal.genericeditor.diff.extension.partitioner; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.rules.*; + +public class DiffPartitionScanner implements IDiffPartitioning, IPartitionTokenScanner { + + private int headerEnd; + private int currentOffset; + private int end; + private int tokenStart; + + @Override + public void setRange(IDocument document, int offset, int length) { + headerEnd = document.get().indexOf("diff --git");//$NON-NLS-1$ + currentOffset = offset; + end = offset + length; + tokenStart = -1; + } + + @Override + public IToken nextToken() { + tokenStart = currentOffset; + if (currentOffset < end) { + if (currentOffset < headerEnd) { + currentOffset = Math.min(headerEnd, end); + return new Token(PARTITION_HEADER); + } else { + currentOffset = end; + return new Token(PARTITION_BODY); + } + } + return Token.EOF; + } + + @Override + public int getTokenOffset() { + return tokenStart; + } + + @Override + public int getTokenLength() { + return currentOffset - tokenStart; + } + + @Override + public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) { + setRange(document, offset, length); + } +} diff --git a/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitioner.java b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitioner.java new file mode 100644 index 000000000..1caf63515 --- /dev/null +++ b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/DiffPartitioner.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. 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 + * + * Contributors: + * Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.team.internal.genericeditor.diff.extension.partitioner; + +import org.eclipse.core.filebuffers.IDocumentSetupParticipant; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.rules.FastPartitioner; +import org.eclipse.jface.text.rules.IPartitionTokenScanner; + +public class DiffPartitioner implements IDocumentSetupParticipant{ + @Override + public void setup(IDocument document) { + IPartitionTokenScanner scanner = new DiffPartitionScanner(); + FastPartitioner partitioner = new FastPartitioner(scanner, IDiffPartitioning.LEGAL_PARTITION_TYPES); + document.setDocumentPartitioner(partitioner); + partitioner.connect(document); + } +} diff --git a/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/IDiffPartitioning.java b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/IDiffPartitioning.java new file mode 100644 index 000000000..a85e77b36 --- /dev/null +++ b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/partitioner/IDiffPartitioning.java @@ -0,0 +1,19 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. 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 + * + * Contributors: + * Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.team.internal.genericeditor.diff.extension.partitioner; + +import org.eclipse.jface.text.IDocument; + +public interface IDiffPartitioning { + public static final String PARTITION_BODY = "__PARTITION_BODY"; //$NON-NLS-1$ + public static final String PARTITION_HEADER = "__PARTITION_HEADER"; //$NON-NLS-1$ + public static final String[] LEGAL_PARTITION_TYPES = new String[] {PARTITION_HEADER,PARTITION_BODY, IDocument.DEFAULT_CONTENT_TYPE }; +} diff --git a/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/presentation/DiffPresentationReconciler.java b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/presentation/DiffPresentationReconciler.java new file mode 100644 index 000000000..9c27d1c18 --- /dev/null +++ b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/presentation/DiffPresentationReconciler.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. 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 + * + * Contributors: + * Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.team.internal.genericeditor.diff.extension.presentation; + +import org.eclipse.jface.text.TextAttribute; +import org.eclipse.jface.text.presentation.PresentationReconciler; +import org.eclipse.jface.text.rules.*; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.team.internal.genericeditor.diff.extension.partitioner.IDiffPartitioning; +import org.eclipse.team.internal.genericeditor.diff.extension.rules.StartOfLineRule; + +/** + * Presentation reconciler collecting different rules for syntax coloring. + */ +public class DiffPresentationReconciler extends PresentationReconciler { + + private final TextAttribute bodyAttributeMinus = new TextAttribute( + Display.getCurrent().getSystemColor(SWT.COLOR_RED)); + private final TextAttribute bodyAttributePlus = new TextAttribute( + Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN)); + private final TextAttribute hunkAttribute = new TextAttribute( + Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE)); + private final TextAttribute headerAttribute = new TextAttribute( + Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA)); + private final TextAttribute indexDiffAttribute = new TextAttribute( + Display.getCurrent().getSystemColor(SWT.COLOR_DARK_CYAN)); + + public DiffPresentationReconciler() { + + RuleBasedScanner hscanner = new RuleBasedScanner(); + IRule[] headerRules=new IRule[3]; + headerRules[0]=new StartOfLineRule("commit", null, new Token(headerAttribute));//$NON-NLS-1$ + headerRules[1]=new StartOfLineRule("Author:", null, new Token(headerAttribute));//$NON-NLS-1$ + headerRules[2]=new StartOfLineRule("Date:", null, new Token(headerAttribute));//$NON-NLS-1$ + hscanner.setRules(headerRules); + DefaultDamagerRepairer hdr = new DefaultDamagerRepairer(hscanner); + this.setDamager(hdr, IDiffPartitioning.PARTITION_HEADER); + this.setRepairer(hdr, IDiffPartitioning.PARTITION_HEADER); + + IRule[] bodyRules=new IRule[7]; + bodyRules[0] = new StartOfLineRule("+", null, new Token(bodyAttributePlus));//$NON-NLS-1$ + bodyRules[1] = new StartOfLineRule("-", null, new Token(bodyAttributeMinus));//$NON-NLS-1$ + bodyRules[2] = new StartOfLineRule("@@", null, new Token(hunkAttribute));//$NON-NLS-1$ + bodyRules[3]=new StartOfLineRule("diff --git", null, new Token(indexDiffAttribute));//$NON-NLS-1$ + bodyRules[4]=new StartOfLineRule("index", null, new Token(indexDiffAttribute));//$NON-NLS-1$ + bodyRules[5]=new StartOfLineRule("---", null, new Token(bodyAttributeMinus));//$NON-NLS-1$ + bodyRules[6]=new StartOfLineRule("+++", null, new Token(bodyAttributePlus));//$NON-NLS-1$ + RuleBasedScanner scanner = new RuleBasedScanner(); + scanner.setRules(bodyRules); + DefaultDamagerRepairer dr = new DefaultDamagerRepairer(scanner); + this.setDamager(dr, IDiffPartitioning.PARTITION_BODY); + this.setRepairer(dr, IDiffPartitioning.PARTITION_BODY); + + } +} diff --git a/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/rules/StartOfLineRule.java b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/rules/StartOfLineRule.java new file mode 100644 index 000000000..c7f993f8f --- /dev/null +++ b/bundles/org.eclipse.team.genericeditor.diff.extension/src/org/eclipse/team/internal/genericeditor/diff/extension/rules/StartOfLineRule.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. 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 + * + * Contributors: + * Sopot Cela (Red Hat Inc.) + *******************************************************************************/ +package org.eclipse.team.internal.genericeditor.diff.extension.rules; + +import org.eclipse.jface.text.rules.*; + +public class StartOfLineRule extends SingleLineRule { + + public StartOfLineRule(String startSequence, String endSequence, IToken token) { + super(startSequence, endSequence, token); + } + + @Override + public IToken evaluate(ICharacterScanner scanner) { + scanner.unread(); + int c = scanner.read(); + if ((c == '\n') || (c == -1)) { + return super.evaluate(scanner); + } + return Token.UNDEFINED; + } +} |