use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method calculateEditLocations.
private WorkspaceJob calculateEditLocations(final NavigableSet<ILocateableNode> nodes, final IFile file, final List<Location> locations_out) throws CoreException {
final WorkspaceJob job = new WorkspaceJob("MinimizeVisibilityRefactoring: calculate edit locations") {
@Override
public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
final int BUF_LEN = 8;
try {
InputStream is = file.getContents();
InputStreamReader isr = new InputStreamReader(is);
int currOffset = 0;
char[] content = new char[BUF_LEN];
for (ILocateableNode node : nodes) {
int toSkip = node.getLocation().getOffset() - currOffset;
if (toSkip < 0) {
ErrorReporter.logError("MinimizeVisibilityRefactoring.ChangeCreator: Negative skip value" + " while parsing file: " + file.getName() + ", offset: " + node.getLocation().getOffset() + "->" + currOffset);
continue;
}
isr.skip(toSkip);
isr.read(content);
String str = new String(content);
VisibilityModifier vm = parseVisibilityModifier(str);
int vmLen = 0;
if (vm != null) {
switch(vm) {
case Public:
vmLen = 7;
break;
case Friend:
vmLen = 7;
break;
case Private:
vmLen = 8;
break;
}
}
locations_out.add(new Location(node.getLocation().getFile(), node.getLocation().getLine(), node.getLocation().getOffset(), node.getLocation().getOffset() + vmLen));
currOffset = node.getLocation().getOffset() + BUF_LEN;
}
isr.close();
is.close();
} catch (IOException ioe) {
ErrorReporter.logError("MinimizeVisibilityRefactoring.CreateChange: " + "Error while reading source project.");
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
return job;
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createFileChange.
private Change createFileChange(final IFile toVisit) {
if (toVisit == null) {
return null;
}
final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
final Module module = sourceParser.containedModule(toVisit);
if (module == null) {
return null;
}
// find all locations in the module that should be edited
final DefinitionVisitor vis = new DefinitionVisitor();
module.accept(vis);
final NavigableSet<ILocateableNode> nodes = vis.getLocations();
if (nodes.isEmpty()) {
return null;
}
// calculate edit locations
final List<Location> locations = new ArrayList<Location>();
try {
final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
job1.join();
} catch (InterruptedException ie) {
ErrorReporter.logExceptionStackTrace(ie);
} catch (CoreException ce) {
ErrorReporter.logError("MinimizeVisibilityRefactoring/CreateChange.createFileChange(): " + "CoreException while calculating edit locations. ");
ErrorReporter.logExceptionStackTrace(ce);
}
if (locations.isEmpty()) {
return null;
}
// create a change for each edit location
final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
final MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
for (Location l : locations) {
final int len = l.getEndOffset() - l.getOffset();
if (len == 0) {
rootEdit.addChild(new InsertEdit(l.getOffset(), "private "));
} else {
rootEdit.addChild(new ReplaceEdit(l.getOffset(), len, "private "));
}
}
return tfc;
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class BlockNode method toString.
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("BN(").append(astNode.toString()).append("), loc: ");
if (astNode instanceof ILocateableNode) {
final Location loc = ((ILocateableNode) astNode).getLocation();
sb.append(loc.getOffset()).append('-').append(loc.getEndOffset()).append(';');
} else {
sb.append("<none>;");
}
sb.append(" parent: ");
if (parent == null) {
sb.append("<null>");
} else {
sb.append("SN(").append(parent.astNode.toString()).append(')');
}
return sb.toString();
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class StatementNode method isLocationEqualTo.
public boolean isLocationEqualTo(final StatementNode sn) {
if (!(this.getAstNode() instanceof ILocateableNode)) {
return false;
}
if (!(sn.getAstNode() instanceof ILocateableNode)) {
return false;
}
final Location loc0 = ((ILocateableNode) this.getAstNode()).getLocation();
final Location loc1 = ((ILocateableNode) sn.getAstNode()).getLocation();
return loc0.getFile().equals(loc1.getFile()) && loc0.getOffset() == loc1.getOffset() && loc0.getEndOffset() == loc1.getEndOffset();
}
use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createFileChange.
private Change createFileChange(final IFile toVisit) {
if (toVisit == null) {
return null;
}
final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
final Module module = sourceParser.containedModule(toVisit);
if (module == null) {
return null;
}
// find all locations in the module that should be edited
final DefinitionVisitor vis = new DefinitionVisitor();
module.accept(vis);
final NavigableSet<Definition> nodes = vis.getLocations();
if (nodes.isEmpty()) {
return null;
}
// calculate edit locations
final List<Definition> locations = new ArrayList<Definition>();
try {
final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
job1.join();
} catch (InterruptedException ie) {
ErrorReporter.logExceptionStackTrace(ie);
} catch (CoreException ce) {
ErrorReporter.logError("UngroupModuleparRefactoring/CreateChange.createFileChange(): " + "CoreException while calculating edit locations. ");
ErrorReporter.logExceptionStackTrace(ce);
}
if (locations.isEmpty()) {
return null;
}
// create a change for each edit location
final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
final MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
int precedeOffset = -1;
final String fileContents = loadFileContent(toVisit);
for (Definition node : locations) {
final Location l = node.getCumulativeDefinitionLocation();
final Location typeLocation = node.getType(CompilationTimeStamp.getBaseTimestamp()).getLocation();
final Location identifierLocation = node.getIdentifier().getLocation();
if (precedeOffset != l.getOffset()) {
precedeOffset = l.getOffset();
final int len = l.getEndOffset() - l.getOffset();
rootEdit.addChild(new DeleteEdit(l.getOffset(), len + 1));
}
String typeText = fileContents.substring(typeLocation.getOffset(), typeLocation.getEndOffset()).trim();
String name = fileContents.substring(identifierLocation.getOffset(), identifierLocation.getEndOffset()).trim();
String newModulePar = "";
if (node instanceof Def_ModulePar) {
Def_ModulePar modulePar = (Def_ModulePar) node;
if (modulePar.getDefaultValue() != null) {
final Location valueLocation = modulePar.getDefaultValue().getLocation();
String valueText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar " + typeText + " " + name + " := " + valueText + ";\n";
} else {
newModulePar = "modulepar " + typeText + " " + name + ";\n";
}
} else if (node instanceof Def_ModulePar_Template) {
Def_ModulePar_Template modulePar = (Def_ModulePar_Template) node;
if (modulePar.getDefaultTemplate() != null) {
final Location valueLocation = modulePar.getDefaultTemplate().getLocation();
String temlateText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar template " + typeText + " " + name + " := " + temlateText + ";\n";
} else {
newModulePar = "modulepar template " + typeText + " " + name + ";\n";
}
}
rootEdit.addChild(new InsertEdit(l.getOffset(), newModulePar));
}
return tfc;
}
Aggregations