Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(position): improve the element positions #984

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/_data/sidebar_doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ entries:
product: all
version: all

- title: Comments
- title: Comments and Positions
url: /comments.html
audience: writers, designers
platform: all
Expand Down
21 changes: 18 additions & 3 deletions doc/comments.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
title: Comments
keywords: comments
last_updated: May 25, 2016
title: Comments and position
keywords: comments position
last_updated: November 25, 2016
---

# Comment

In Spoon there are four different kinds of comments:

* File comments (comment at the begin of the file, generally licence) `CtComment.CommentType.FILE`
Expand Down Expand Up @@ -92,3 +94,16 @@ public class CtCommentProcessor extends AbstractProcessor<CtComment> {
}
}
```

# Source Position

`SourcePosition` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/cu/SourcePosition.html)) defines the position of the `CtElement` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/declaration/CtElement.html)) in the original source file.
SourcePosition is extended by three specialized positions:

- `DeclarationSourcePosition` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/cu/position/DeclarationSourcePosition.html))
- `BodyHolderSourcePosition` ([javadoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/cu/position/BodyHolderSourcePosition.html)).

These three specializations are used to define the position of specific CtElement.
For example DeclarationSourcePosition is used to define the position of all declarations (variable, type, method, ...).
This provide an easy access to the position of the modifiers and the name.
The BodyHolderSourcePosition is used to declare the position of all elements that have a body.
15 changes: 7 additions & 8 deletions src/main/java/spoon/reflect/cu/SourcePosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
*/
package spoon.reflect.cu;

import java.io.File;

import spoon.compiler.Environment;
import spoon.reflect.cu.position.NoSourcePosition;

import java.io.File;
import java.io.Serializable;

/**
* This interface represents the position of a program element in a source file.
*/
public interface SourcePosition {
public interface SourcePosition extends Serializable {

SourcePosition NOPOSITION = new NoSourcePosition();

/**
* Returns a string representation of this position in the form
Expand Down Expand Up @@ -79,9 +83,4 @@ public interface SourcePosition {
* Gets the index at which the position starts in the source file.
*/
int getSourceStart();

/**
* Gets the index at which the name of the element position starts in the source file.
*/
int getNameSourceStart();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

/**
* This interface represents the position of a Method declaration in a source file.
*/
public interface BodyHolderSourcePosition extends DeclarationSourcePosition {

int getBodyStart();

int getBodyEnd();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

import spoon.reflect.cu.SourcePosition;

/**
* This interface represents the position of a program element in a source file.
*/
public interface DeclarationSourcePosition extends SourcePosition {

int getModifierSourceStart();

int getModifierSourceEnd();

int getNameStart();

int getNameEnd();

}
76 changes: 76 additions & 0 deletions src/main/java/spoon/reflect/cu/position/NoSourcePosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.cu.position;

import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;

import java.io.File;
import java.io.Serializable;

/**
* This interface represents the position of a program element in a source file.
*/
public class NoSourcePosition implements SourcePosition, Serializable {

private static final long serialVersionUID = 1L;

@Override
public File getFile() {
return null;
}

@Override
public CompilationUnit getCompilationUnit() {
return null;
}

@Override
public int getLine() {
return -1;
}

@Override
public int getEndLine() {
return -1;
}

@Override
public int getColumn() {
return -1;
}

@Override
public int getEndColumn() {
return -1;
}

@Override
public int getSourceEnd() {
return -1;
}

@Override
public int getSourceStart() {
return -1;
}

@Override
public String toString() {
return "(unknown file)";
}
}
30 changes: 30 additions & 0 deletions src/main/java/spoon/reflect/factory/CoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.cu.position.BodyHolderSourcePosition;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
Expand Down Expand Up @@ -374,10 +376,38 @@ public interface CoreFactory {
/**
* Creates a source position.
*/
@Deprecated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourcePosition createSourcePosition(
CompilationUnit compilationUnit,
int startDeclaration, int startSource, int end, int[] lineSeparatorPositions);

/**
* Creates a source position.
*/
SourcePosition createSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end, int[] lineSeparatorPositions);

/**
* Creates a declaration source position.
*/
DeclarationSourcePosition createDeclarationSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int modifierStart, int modifierEnd,
int declarationStart, int declarationEnd,
int[] lineSeparatorPositions);

/**
* Creates a body holder source position.
*/
BodyHolderSourcePosition createBodyHolderSourcePosition(
CompilationUnit compilationUnit,
int startSource, int end,
int modifierStart, int modifierEnd,
int declarationStart, int declarationEnd,
int bodyStart, int bodyEnd, int[] lineSeparatorPositions);

/**
* Creates a statement list.
*/
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/spoon/support/DefaultCoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.BodyHolderSourcePosition;
import spoon.reflect.cu.position.DeclarationSourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
Expand Down Expand Up @@ -140,7 +142,9 @@
import spoon.support.reflect.code.CtVariableWriteImpl;
import spoon.support.reflect.code.CtWhileImpl;
import spoon.support.reflect.cu.CompilationUnitImpl;
import spoon.support.reflect.cu.SourcePositionImpl;
import spoon.support.reflect.cu.position.BodyHolderSourcePositionImpl;
import spoon.support.reflect.cu.position.DeclarationSourcePositionImpl;
import spoon.support.reflect.cu.position.SourcePositionImpl;
import spoon.support.reflect.declaration.CtAnnotationImpl;
import spoon.support.reflect.declaration.CtAnnotationMethodImpl;
import spoon.support.reflect.declaration.CtAnnotationTypeImpl;
Expand Down Expand Up @@ -637,8 +641,30 @@ public void setMainFactory(Factory mainFactory) {
this.factory = mainFactory;
}

@Override
@Deprecated
public SourcePosition createSourcePosition(CompilationUnit compilationUnit, int startDeclaration, int startSource, int end, int[] lineSeparatorPositions) {
return new SourcePositionImpl(compilationUnit, startDeclaration, startSource, end, lineSeparatorPositions);
return new SourcePositionImpl(compilationUnit, startSource, end, lineSeparatorPositions);
}

@Override
public SourcePosition createSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int[] lineSeparatorPositions) {
return new SourcePositionImpl(compilationUnit, startSource, end, lineSeparatorPositions);
}

@Override
public DeclarationSourcePosition createDeclarationSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int modifierStart, int modifierEnd, int declarationStart, int declarationEnd, int[] lineSeparatorPositions) {
return new DeclarationSourcePositionImpl(compilationUnit, startSource, end, modifierStart, modifierEnd, declarationStart, declarationEnd, lineSeparatorPositions);
}

@Override
public BodyHolderSourcePosition createBodyHolderSourcePosition(CompilationUnit compilationUnit, int startSource, int end, int modifierStart, int modifierEnd, int declarationStart, int declarationEnd, int bodyStart, int bodyEnd, int[] lineSeparatorPositions) {
return new BodyHolderSourcePositionImpl(compilationUnit,
startSource, end,
modifierStart, modifierEnd,
declarationStart, declarationEnd,
bodyStart, bodyEnd,
lineSeparatorPositions);
}

public CompilationUnit createCompilationUnit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void buildComment(int[] positions) {
String commentContent = getCommentContent(start, end);

int[] lineSeparatorPositions = declarationUnit.compilationResult.lineSeparatorPositions;
SourcePosition sourcePosition = factory.Core().createSourcePosition(spoonUnit, start, start, end, lineSeparatorPositions);
SourcePosition sourcePosition = factory.Core().createSourcePosition(spoonUnit, start, end, lineSeparatorPositions);

// create the Spoon comment element
comment.setContent(commentContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -285,6 +284,8 @@ <T> CtVariableAccess<T> createVariableAccess(QualifiedNameReference qualifiedNam
va = createVariableAccess(ref, isOtherBinding && fromAssignment);
}

ref.setPosition(jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));

if (qualifiedNameReference.otherBindings != null) {
int i = 0; //positions index;
va.setPosition(ref.getPosition());
Expand All @@ -310,10 +311,8 @@ <T> CtVariableAccess<T> createVariableAccess(QualifiedNameReference qualifiedNam
CtFieldAccess<T> other = createFieldAccess(//
jdtTreeBuilder.getReferencesBuilder().<T>getVariableReference(null, qualifiedNameReference.tokens[i]), va, isOtherBinding && fromAssignment);
//set source position of va;
CompilationUnit cu = jdtTreeBuilder.getFactory().CompilationUnit().create(new String(jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
sourceEnd = (int) (positions[i]);
final int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
va.setPosition(jdtTreeBuilder.getFactory().Core().createSourcePosition(cu, sourceStart, sourceStart, sourceEnd, lineSeparatorPositions));
va.setPosition(jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
va = other;
}
}
Expand Down
Loading