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

V3.0 #6

Open
wants to merge 2 commits into
base: v3.0
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions pdt.refactoring/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: PDT Refactoring
Bundle-SymbolicName: org.cs3.pdt.refactoring
Bundle-SymbolicName: org.cs3.pdt.refactoring;singleton:=true
Bundle-Version: 3.0.0.qualifier
Bundle-Activator: org.cs3.pdt.refactoring.PDTRefactoringPlugin
Require-Bundle: org.eclipse.ui,
Expand All @@ -10,6 +10,15 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ltk.core.refactoring,
org.eclipse.ltk.ui.refactoring,
org.cs3.pdt.common;bundle-version="[3.0.0,4.0.0)",
org.eclipse.jface.text
org.eclipse.jface.text,
org.eclipse.ui.workbench,
org.cs3.pdt.editor;bundle-version="3.0.0",
org.eclipse.ui.workbench.texteditor;bundle-version="3.8.101",
org.eclipse.ui.editors
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Import-Package: org.cs3.jtransformer.util,
org.cs3.pdt.editor.internal.editors,
org.eclipse.jdt.ui,
org.eclipse.ui,
org.eclipse.ui.editors.text
3 changes: 2 additions & 1 deletion pdt.refactoring/build.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
.,\
plugin.xml
3 changes: 3 additions & 0 deletions pdt.refactoring/doc/todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- automatically consult the files in the pl folder
- Change "Refactoring" to "Renaming" and think about a concept to include various refacorings
- Create Test suites
2 changes: 2 additions & 0 deletions pdt.refactoring/pl/SamplePrologFile.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
isCity(bonn).
isCity(copenhagen).
4 changes: 4 additions & 0 deletions pdt.refactoring/pl/load.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:- use_module('prolog_refactoring_demo_renaming',
[ rename_predicate/6 % (+M:N/A, +NewName, ?File, ?Start, ?End, ?NewText)
]
).
58 changes: 58 additions & 0 deletions pdt.refactoring/pl/prolog_refactoring_demo_renaming.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
:- module(prolog_refactoring_demo_renaming, [
rename_predicate/6 % (+M:N/A, +NewName, ?File, ?Start, ?End, ?NewText)
]).

:- use_module(library(prolog_clause)).
:- use_module(pdt_common_pl('callgraph/pdt_call_graph')).

:- dynamic(result/4).

assert_result(Goal, Caller, From, CallKind) :-
assertz(result(Goal, Caller, From, CallKind)).

%% rename_predicate(+PI, +NewName, File, Start, End, NewText)
%
% Rename M:N/A replacing N by NewName.
% File, Start, End and NewText form a Text Change.
% In the case of a predicate renaming Length is the Length of N
% and NewText = NewName in all TextChange values.

rename_predicate(M:N/A, NewName, File, Start, End, NewText) :-
NewText = NewName,
position_of(M:N/A, File, Start, End).

% We need all declarations (predicate heads) and calls of M:N/A.
% F is the file and S is the start position of N in the file:
position_of(M:N/A, File, Start, End) :- declaration_positon(M:N/A, File, Start, End).
position_of(M:N/A, File, Start, End) :- call_positon( M:N/A, File, Start, End).


declaration_positon(M:N/A, File, Start, End) :-
functor(Head, N, A),
clause(M:Head, _, Ref),
clause_info(Ref, File, TermPosition, _),
( clause_property(Ref, fact)
-> % fact
TermPosition = HeadPosition
; % clause with body
TermPosition = term_position(_, _, _, _, [HeadPosition|_])
),
( HeadPosition = Start-End
-> % no arguments
true
; % at least one argument
HeadPosition = term_position(_, _, Start, End, _)
).

call_positon(M:N/A, File, Start, End) :-
retractall(result(_, _, _, _)),
functor(Head, N, A),
pdt_walk_code([trace_reference(M:Head), on_trace(prolog_refactoring_demo_renaming:assert_result)]),
!,
retract(result(_Goal, _Caller, From, _CallKind)),
From = clause_term_position(Ref, TermPosition),
clause_property(Ref, file(File)),
( TermPosition = Start-End
-> true
; TermPosition = term_position(_, _, Start, End, _)
).
55 changes: 55 additions & 0 deletions pdt.refactoring/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.commands">
<command categoryId="org.cs3.pdt.editing" id="org.cs3.pdt.refactoring.rename" name="Prolog Refactor" />
</extension>

<extension point="org.eclipse.ui.handlers">
<handler commandId="org.cs3.pdt.refactoring.rename" class="org.cs3.pdt.refactoring.PDTRefactoringHandler" />
</extension>

<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.ui.popup.any?before=additions">
<menu id="org.cs3.pdt.refactoring.RefactoringMenu" label="Prolog Refactoring">
<command commandId="org.cs3.pdt.refactoring.rename" id="org.cs3.pdt.refactoring.rename.popupmenu" label="Rename Prolog Element...">
<visibleWhen checkEnabled="false">
<with variable="selection">
<iterate ifEmpty="false" operator="or">
<or>
<adapt type="org.eclipse.core.resources.IResource">
<or>
<test property="org.eclipse.core.resources.extension" value="pl" />
</or>
</adapt>
<with variable="activeEditorInput">
<adapt type="org.eclipse.core.resources.IResource">
<or>
<test property="org.eclipse.core.resources.extension" value="pl" />
</or>
</adapt>
</with>
</or>
</iterate>
</with>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>


<!-- The prolog files that will be consulted. -->
<extension point="org.cs3.pdt.connector.bootstrapContribution">
<fileContribution
id="pdt.refactoring.contribution"
path="pl/load.pl"
key="">
<dependency
contribution="org.cs3.pdt.common.fileContribution">
</dependency>
</fileContribution>
</extension>


</plugin>
163 changes: 150 additions & 13 deletions pdt.refactoring/src/org/cs3/pdt/refactoring/PDTRefactoring.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,164 @@
package org.cs3.pdt.refactoring;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cs3.pdt.common.PDTCommonUtil;
import org.cs3.pdt.common.metadata.Goal;
import org.cs3.pdt.connector.util.FileUtils;
import org.cs3.pdt.editor.internal.editors.PLEditor;
import org.cs3.prolog.connector.common.Debug;
import org.cs3.prolog.connector.common.QueryUtils;
import org.cs3.prolog.connector.process.PrologProcess;
import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
import org.cs3.prolog.connector.process.PrologProcessException;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.ui.handlers.HandlerUtil;


public class PDTRefactoring {
public class PDTRefactoring extends Refactoring{

public static final String VARIABLE_REPLACEMENT = "Replacement";
public static final String VARIABLE_OFFSET_END = "OffsetEnd";
public static final String VARIABLE_OFFSET_START = "OffsetStart";
public static final String VARIABLE_NEW_TEXT = "NewText";
public static final String VARIABLE_OFFSET_END = "End";
public static final String VARIABLE_OFFSET_START = "Start";
public static final String VARIABLE_FILE = "File";

public static final String REFACTORING_NAME = "PDT Refactoring";

public static int runRefactoringWithQuery(Shell shell, String query) throws InterruptedException {
return runRefactoringWithQuery(shell, PDTCommonUtil.getActivePrologProcess(), query);
}
private PDTRefactoringWizard wizard;
private PrologProcess prologProcess;
private PLEditor editor;

private Goal goal;

public static int runRefactoringWithQuery(Shell shell, PrologProcess process, String query) throws InterruptedException {
ProcessorBasedRefactoring refactoring = new ProcessorBasedRefactoring(new PDTRefactoringProcessor(process, query));
PDTRefactoringWizard wizard = new PDTRefactoringWizard(refactoring);
public void init(ExecutionEvent event){

editor = (PLEditor)HandlerUtil.getActiveEditor(event);

// Get a reference to the running prolog process
prologProcess = PDTCommonUtil.getActivePrologProcess();
//TODO this should be done asyncronously and I need a method be be sure it finished
try {
prologProcess.start();
} catch (PrologProcessException e1) {
e1.printStackTrace();
}

// Create the Refactoring wizard...
wizard = new PDTRefactoringWizard(this);

// Start the wizard!
RefactoringWizardOpenOperation operation = new RefactoringWizardOpenOperation(wizard);
return operation.run(shell, "PDT Refactoring");

try {
operation.run(HandlerUtil.getActiveShell(event), this.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}

}



@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
throws CoreException, OperationCanceledException {

return new RefactoringStatus();
}

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
throws CoreException, OperationCanceledException {

return new RefactoringStatus();
}



@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
String newText = wizard.getNewText();
// The prolog predicate that is to be renamed

Display.getDefault().syncExec(new Runnable() {
public void run() {
try {
goal = editor.getSelectedPrologElement();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
});


String query = QueryUtils.bT("rename_predicate", "user:"+goal.toString(),"'"+newText+"'",
VARIABLE_FILE,VARIABLE_OFFSET_START,VARIABLE_OFFSET_END,VARIABLE_NEW_TEXT);



CompositeChange change = null;
try {
List<Map<String, Object>> results = prologProcess.queryAll(query);
System.out.println(results);
change = new CompositeChange("Replacements");
HashMap<String, TextFileChange> changes = new HashMap<>();
for (Map<String, Object> result : results) {
try {
//TODO Check for exceptional results
//TODO instanceof check
String pathOut = (String) result.get(VARIABLE_FILE);
int startOut = Integer.parseInt((String) result.get(VARIABLE_OFFSET_START));
int endOut = Integer.parseInt((String) result.get(VARIABLE_OFFSET_END));
String replacementOut = (String) result.get(VARIABLE_NEW_TEXT);
int offset = startOut;
int length = endOut-startOut;


//path = Util.unquoteAtom(path); TODO: What do I need this for?
TextFileChange textFileChange = changes.get(pathOut);
if (textFileChange == null) {
//IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
// new Path(pathOut).makeAbsolute());
IFile file = FileUtils.findFileForLocation(pathOut);
textFileChange = new TextFileChange(file.getName(), file);
MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
textFileChange.setEdit(fileChangeRootEdit);
changes.put(pathOut, textFileChange);
change.add(textFileChange);

}

ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementOut);
textFileChange.addEdit(replaceEdit);
} catch (Exception e) {
Debug.report(e);
}
}
} catch (PrologProcessException e) {
Debug.report(e);
}
return change;
}

@Override
public String getName() {
return REFACTORING_NAME;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cs3.pdt.refactoring;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

public class PDTRefactoringHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

new PDTRefactoring().init(event);

return null;
}

}
Loading