1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 from catapult_base
.refactor
.annotated_symbol
import base_symbol
10 from catapult_base
.refactor
import snippet
18 class Reference(base_symbol
.AnnotatedSymbol
):
20 def Annotate(cls
, nodes
):
23 if nodes
[0].type != symbol
.atom
:
25 if not nodes
[0].children
or nodes
[0].children
[0].type != token
.NAME
:
28 for i
in xrange(1, len(nodes
)):
31 if nodes
[i
].type != symbol
.trailer
:
33 if len(nodes
[i
].children
) != 2:
35 if (nodes
[i
].children
[0].type != token
.DOT
or
36 nodes
[i
].children
[1].type != token
.NAME
):
41 return [cls(nodes
[:i
])] + nodes
[i
:]
43 def __init__(self
, children
):
44 super(Reference
, self
).__init
__(-1, children
)
48 return 'attribute_reference'
52 return ''.join(token_snippet
.value
53 for child
in self
.children
54 for token_snippet
in child
.children
)
57 def value(self
, value
):
58 value_parts
= value
.split('.')
60 # If we have too many children, cut the list down to size.
61 self
._children
= self
._children
[:len(value_parts
)]
64 for child
, value_part
in itertools
.izip_longest(
65 self
._children
, value_parts
):
67 # Modify existing children. This helps preserve comments and spaces.
68 child
.children
[-1].value
= value_part
70 # Add children as needed.
72 snippet
.TokenSnippet
.Create(token
.DOT
, '.'),
73 snippet
.TokenSnippet
.Create(token
.NAME
, value_part
),
75 self
._children
.append(snippet
.Symbol(symbol
.trailer
, token_snippets
))