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

Allow statements as well as structures and tokens to be inserted adjacent to a token #151

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

karenetheridge
Copy link
Contributor

Otherwise, this would be impossible:

my $doc = PPI::Document->new(\ "1;\n");
my $token = $doc->last_element;

my $new_document = PPI::Document->new(\ '$foo = 1;');
$token->insert_after($_) foreach reverse $new_document->schildren;

guillaumeaubert and others added 22 commits November 11, 2014 22:13
Conflicts:
	t/ppi_statement_sub.t
Conflicts:
	t/ppi_statement_package.t
	t/ppi_statement_sub.t
	t/ppi_token_operator.t
…cent to a token

Otherwise, this would be impossible:

    my $doc = PPI::Document->new(\ "1;\n");
    my $token = $doc->last_element;

    my $new_document = PPI::Document->new(\ '$foo = 1;');
    $token->insert_after($_) foreach reverse $new_document->schildren;
@adamkennedy
Copy link
Collaborator

I'm not sure it can be this simple.

You need tests for this, because I'm fairly certain this will allow breaking the structural integrity rules. You end up with statement/statement children, and possibly structure/structure children as well.

The token you insert after has to be insignificant and not be a direct child of the same type of thing you are inserting.

But that said, I'm really excited to see someone padding document manipulation functionality forward. Thank you so much.

@karenetheridge
Copy link
Contributor Author

Hmm ok, I guess I picked the wrong solution to the problem.

Let me back up and restate the problem:

"I have a parsed PPI::Document. I have a snippet of code, in a string, that I wish to insert at a specific point (let's say the end, for simplicity) in the document. How can I do that?" The code example in the top post was my best guess at a solution, but it only works if the new code snippet parses to specific types of nodes (e.g. tokens), and not other types (a statement).

@adamkennedy
Copy link
Collaborator

I had always intended PPI::Document::Fragment to represent arbitrary chunks
of code that may not be serialisable out to as a full document (so they
have a tree but no line numbers etc) , and would hold the complex logic
needed to automate insertion into documents properly.

Adam
On Dec 19, 2014 5:46 AM, "Karen Etheridge" notifications@github.com wrote:

Hmm ok, I guess I picked the wrong solution to the problem.

Let me back up and restate the problem:

"I have a parsed PPI::Document. I have a snippet of code, in a string,
that I wish to insert at a specific point in the document. How can I do
that?" The code example in the top post was my best guess at a solution,
but it only works if the new code snippet parses to specific types of nodes
(e.g. tokens), and not other types (a statement).


Reply to this email directly or view it on GitHub
#151 (comment).

@adamkennedy
Copy link
Collaborator

By the way, that idea of serialisable or not is why you can't save
arbitrary statements to files. Without identifying something as a full
blown document, things like heredocs won't unroll correctly.

Fragments would not be expected to serialize in this way, freeing them up
to be moved and manipulated much more freely and becoming the main vessel
for cut/copy/paste operations and the like.

Adam
On Dec 19, 2014 5:46 AM, "Karen Etheridge" notifications@github.com wrote:

Hmm ok, I guess I picked the wrong solution to the problem.

Let me back up and restate the problem:

"I have a parsed PPI::Document. I have a snippet of code, in a string,
that I wish to insert at a specific point in the document. How can I do
that?" The code example in the top post was my best guess at a solution,
but it only works if the new code snippet parses to specific types of nodes
(e.g. tokens), and not other types (a statement).


Reply to this email directly or view it on GitHub
#151 (comment).

@wchristian
Copy link
Member

wchristian commented Jul 20, 2022

This does need tests and also particularly tests of the type of "what happens if the document you're inserting to is a partial perl document and the last element is an unclosed string in the middle of a function call in the middle of a subrouting? how does this interact with serializing?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants