2015-04-21 21:01:19 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
use Shape;
|
2016-07-26 06:20:01 +01:00
|
|
|
use utils;
|
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
2016-05-25 20:41:26 +02:00
|
|
|
use codemap::SpanUtils;
|
2015-10-06 22:13:14 +02:00
|
|
|
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
|
2017-01-19 10:47:07 +13:00
|
|
|
use types::{rewrite_path, PathContext};
|
2015-07-17 15:58:47 +02:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2016-07-26 06:20:01 +01:00
|
|
|
use visitor::FmtVisitor;
|
2016-08-10 08:13:27 +02:00
|
|
|
use std::cmp::{self, Ordering};
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2016-07-26 06:20:01 +01:00
|
|
|
use syntax::{ast, ptr};
|
|
|
|
|
|
|
|
fn path_of(a: &ast::ViewPath_) -> &ast::Path {
|
2016-08-23 23:14:45 +09:00
|
|
|
match *a {
|
|
|
|
ast::ViewPath_::ViewPathSimple(_, ref p) => p,
|
|
|
|
ast::ViewPath_::ViewPathGlob(ref p) => p,
|
|
|
|
ast::ViewPath_::ViewPathList(ref p, _) => p,
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_path_segments(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
|
2017-04-24 16:50:11 +09:00
|
|
|
a.identifier.name.as_str().cmp(&b.identifier.name.as_str())
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_paths(a: &ast::Path, b: &ast::Path) -> Ordering {
|
|
|
|
for segment in a.segments.iter().zip(b.segments.iter()) {
|
|
|
|
let ord = compare_path_segments(segment.0, segment.1);
|
|
|
|
if ord != Ordering::Equal {
|
|
|
|
return ord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.segments.len().cmp(&b.segments.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_path_list_items(a: &ast::PathListItem, b: &ast::PathListItem) -> Ordering {
|
2017-03-28 11:14:47 +13:00
|
|
|
let a_name_str = &*a.node.name.name.as_str();
|
|
|
|
let b_name_str = &*b.node.name.name.as_str();
|
2016-09-16 15:19:18 +12:00
|
|
|
let name_ordering = if a_name_str == "self" {
|
|
|
|
if b_name_str == "self" {
|
|
|
|
Ordering::Equal
|
|
|
|
} else {
|
|
|
|
Ordering::Less
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
2016-09-16 15:19:18 +12:00
|
|
|
} else {
|
|
|
|
if b_name_str == "self" {
|
|
|
|
Ordering::Greater
|
|
|
|
} else {
|
|
|
|
a_name_str.cmp(&b_name_str)
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if name_ordering == Ordering::Equal {
|
2016-09-16 15:19:18 +12:00
|
|
|
match a.node.rename {
|
2016-07-26 06:20:01 +01:00
|
|
|
Some(a_rename) => {
|
2016-09-16 15:19:18 +12:00
|
|
|
match b.node.rename {
|
2017-03-28 11:25:59 +13:00
|
|
|
Some(b_rename) => a_rename.name.as_str().cmp(&b_rename.name.as_str()),
|
2016-07-26 06:20:01 +01:00
|
|
|
None => Ordering::Greater,
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 15:19:18 +12:00
|
|
|
None => Ordering::Less,
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name_ordering
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_path_list_item_lists(a_items: &Vec<ast::PathListItem>,
|
|
|
|
b_items: &Vec<ast::PathListItem>)
|
|
|
|
-> Ordering {
|
|
|
|
let mut a = a_items.clone();
|
|
|
|
let mut b = b_items.clone();
|
|
|
|
a.sort_by(|a, b| compare_path_list_items(a, b));
|
|
|
|
b.sort_by(|a, b| compare_path_list_items(a, b));
|
|
|
|
for comparison_pair in a.iter().zip(b.iter()) {
|
|
|
|
let ord = compare_path_list_items(comparison_pair.0, comparison_pair.1);
|
|
|
|
if ord != Ordering::Equal {
|
|
|
|
return ord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.len().cmp(&b.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_view_path_types(a: &ast::ViewPath_, b: &ast::ViewPath_) -> Ordering {
|
|
|
|
use syntax::ast::ViewPath_::*;
|
|
|
|
match (a, b) {
|
|
|
|
(&ViewPathSimple(..), &ViewPathSimple(..)) => Ordering::Equal,
|
|
|
|
(&ViewPathSimple(..), _) => Ordering::Less,
|
|
|
|
(&ViewPathGlob(_), &ViewPathSimple(..)) => Ordering::Greater,
|
|
|
|
(&ViewPathGlob(_), &ViewPathGlob(_)) => Ordering::Equal,
|
|
|
|
(&ViewPathGlob(_), &ViewPathList(..)) => Ordering::Less,
|
|
|
|
(&ViewPathList(_, ref a_items), &ViewPathList(_, ref b_items)) => {
|
|
|
|
compare_path_list_item_lists(a_items, b_items)
|
|
|
|
}
|
|
|
|
(&ViewPathList(..), _) => Ordering::Greater,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_view_paths(a: &ast::ViewPath_, b: &ast::ViewPath_) -> Ordering {
|
|
|
|
match compare_paths(path_of(a), path_of(b)) {
|
|
|
|
Ordering::Equal => compare_view_path_types(a, b),
|
|
|
|
cmp => cmp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare_use_items(a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
|
|
|
|
match (&a.node, &b.node) {
|
|
|
|
(&ast::ItemKind::Use(ref a_vp), &ast::ItemKind::Use(ref b_vp)) => {
|
|
|
|
Some(compare_view_paths(&a_vp.node, &b_vp.node))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-08-16 15:58:17 +12:00
|
|
|
// TODO (some day) remove unused imports, expand globs, compress many single
|
|
|
|
// imports into a list import.
|
2015-04-21 22:50:43 +12:00
|
|
|
|
2017-03-08 10:10:56 +13:00
|
|
|
fn rewrite_view_path_prefix(path: &ast::Path,
|
|
|
|
context: &RewriteContext,
|
|
|
|
shape: Shape)
|
|
|
|
-> Option<String> {
|
2017-03-28 11:25:59 +13:00
|
|
|
let path_str = if path.segments.last().unwrap().identifier.to_string() == "self" &&
|
|
|
|
path.segments.len() > 1 {
|
2017-03-08 10:10:56 +13:00
|
|
|
let path = &ast::Path {
|
2017-04-15 18:42:45 +09:00
|
|
|
span: path.span.clone(),
|
|
|
|
segments: path.segments[..path.segments.len() - 1].to_owned(),
|
|
|
|
};
|
2017-03-08 10:10:56 +13:00
|
|
|
try_opt!(rewrite_path(context, PathContext::Import, None, path, shape))
|
|
|
|
} else {
|
|
|
|
try_opt!(rewrite_path(context, PathContext::Import, None, path, shape))
|
|
|
|
};
|
|
|
|
Some(path_str)
|
|
|
|
}
|
|
|
|
|
2015-07-17 15:58:47 +02:00
|
|
|
impl Rewrite for ast::ViewPath {
|
|
|
|
// Returns an empty string when the ViewPath is empty (like foo::bar::{})
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-07-17 15:58:47 +02:00
|
|
|
match self.node {
|
2015-09-04 18:09:05 +02:00
|
|
|
ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => {
|
|
|
|
Some(String::new())
|
|
|
|
}
|
2015-07-17 15:58:47 +02:00
|
|
|
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
rewrite_use_list(shape, path, path_list, self.span, context)
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
2017-03-08 10:10:56 +13:00
|
|
|
ast::ViewPath_::ViewPathGlob(ref path) => {
|
|
|
|
// 4 = "::*".len()
|
|
|
|
let prefix_shape = try_opt!(shape.sub_width(3));
|
|
|
|
let path_str = try_opt!(rewrite_view_path_prefix(path, context, prefix_shape));
|
|
|
|
Some(format!("{}::*", path_str))
|
|
|
|
}
|
2015-07-25 23:10:48 +02:00
|
|
|
ast::ViewPath_::ViewPathSimple(ident, ref path) => {
|
2015-08-14 14:09:19 +02:00
|
|
|
let ident_str = ident.to_string();
|
|
|
|
// 4 = " as ".len()
|
2017-03-08 10:10:56 +13:00
|
|
|
let prefix_shape = try_opt!(shape.sub_width(ident_str.len() + 4));
|
|
|
|
let path_str = try_opt!(rewrite_view_path_prefix(path, context, prefix_shape));
|
2015-07-25 23:10:48 +02:00
|
|
|
|
2017-03-28 11:14:47 +13:00
|
|
|
Some(if path.segments.last().unwrap().identifier == ident {
|
2017-02-22 16:20:50 +13:00
|
|
|
path_str
|
|
|
|
} else {
|
|
|
|
format!("{} as {}", path_str, ident_str)
|
|
|
|
})
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:20:01 +01:00
|
|
|
impl<'a> FmtVisitor<'a> {
|
|
|
|
pub fn format_imports(&mut self, use_items: &[ptr::P<ast::Item>]) {
|
2016-08-10 08:13:27 +02:00
|
|
|
// Find the location immediately before the first use item in the run. This must not lie
|
|
|
|
// before the current `self.last_pos`
|
2017-03-28 10:58:41 +13:00
|
|
|
let pos_before_first_use_item = use_items
|
|
|
|
.first()
|
2016-11-03 04:21:47 +00:00
|
|
|
.map(|p_i| {
|
2017-03-22 09:05:50 +13:00
|
|
|
cmp::max(self.last_pos,
|
|
|
|
p_i.attrs
|
|
|
|
.iter()
|
|
|
|
.map(|attr| attr.span.lo)
|
|
|
|
.min()
|
|
|
|
.unwrap_or(p_i.span.lo))
|
|
|
|
})
|
2016-08-03 01:25:54 +03:00
|
|
|
.unwrap_or(self.last_pos);
|
2016-08-10 08:13:27 +02:00
|
|
|
// Construct a list of pairs, each containing a `use` item and the start of span before
|
|
|
|
// that `use` item.
|
|
|
|
let mut last_pos_of_prev_use_item = pos_before_first_use_item;
|
2017-03-28 10:58:41 +13:00
|
|
|
let mut ordered_use_items = use_items
|
|
|
|
.iter()
|
2016-07-26 06:20:01 +01:00
|
|
|
.map(|p_i| {
|
2017-02-23 08:52:52 +13:00
|
|
|
let new_item = (&*p_i, last_pos_of_prev_use_item);
|
|
|
|
last_pos_of_prev_use_item = p_i.span.hi;
|
|
|
|
new_item
|
|
|
|
})
|
2016-07-26 06:20:01 +01:00
|
|
|
.collect::<Vec<_>>();
|
2016-08-10 08:13:27 +02:00
|
|
|
let pos_after_last_use_item = last_pos_of_prev_use_item;
|
2016-07-26 06:20:01 +01:00
|
|
|
// Order the imports by view-path & other import path properties
|
|
|
|
ordered_use_items.sort_by(|a, b| compare_use_items(a.0, b.0).unwrap());
|
|
|
|
// First, output the span before the first import
|
2016-08-24 21:32:04 +01:00
|
|
|
let prev_span_str = self.snippet(codemap::mk_sp(self.last_pos, pos_before_first_use_item));
|
|
|
|
// Look for purely trailing space at the start of the prefix snippet before a linefeed, or
|
|
|
|
// a prefix that's entirely horizontal whitespace.
|
|
|
|
let prefix_span_start = match prev_span_str.find('\n') {
|
|
|
|
Some(offset) if prev_span_str[..offset].trim().is_empty() => {
|
|
|
|
self.last_pos + BytePos(offset as u32)
|
|
|
|
}
|
|
|
|
None if prev_span_str.trim().is_empty() => pos_before_first_use_item,
|
|
|
|
_ => self.last_pos,
|
|
|
|
};
|
2016-08-10 08:13:27 +02:00
|
|
|
// Look for indent (the line part preceding the use is all whitespace) and excise that
|
|
|
|
// from the prefix
|
|
|
|
let span_end = match prev_span_str.rfind('\n') {
|
2016-08-24 21:32:04 +01:00
|
|
|
Some(offset) if prev_span_str[offset..].trim().is_empty() => {
|
|
|
|
self.last_pos + BytePos(offset as u32)
|
2016-08-10 08:13:27 +02:00
|
|
|
}
|
2016-08-24 21:32:04 +01:00
|
|
|
_ => pos_before_first_use_item,
|
2016-08-10 08:13:27 +02:00
|
|
|
};
|
2016-08-24 21:32:04 +01:00
|
|
|
|
|
|
|
self.last_pos = prefix_span_start;
|
2016-08-10 08:13:27 +02:00
|
|
|
self.format_missing(span_end);
|
2016-07-26 06:20:01 +01:00
|
|
|
for ordered in ordered_use_items {
|
|
|
|
// Fake out the formatter by setting `self.last_pos` to the appropriate location before
|
|
|
|
// each item before visiting it.
|
|
|
|
self.last_pos = ordered.1;
|
2016-08-23 23:14:45 +09:00
|
|
|
self.visit_item(ordered.0);
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
2016-08-10 08:13:27 +02:00
|
|
|
self.last_pos = pos_after_last_use_item;
|
2016-07-26 06:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
|
|
|
|
let vis = utils::format_visibility(vis);
|
|
|
|
let mut offset = self.block_indent;
|
|
|
|
offset.alignment += vis.len() + "use ".len();
|
|
|
|
// 1 = ";"
|
|
|
|
match vp.rewrite(&self.get_context(),
|
2017-05-16 15:47:09 +07:00
|
|
|
Shape::legacy(self.config.max_width() - offset.width() - 1, offset)) {
|
2016-07-26 06:20:01 +01:00
|
|
|
Some(ref s) if s.is_empty() => {
|
|
|
|
// Format up to last newline
|
|
|
|
let prev_span = codemap::mk_sp(self.last_pos, source!(self, span).lo);
|
|
|
|
let span_end = match self.snippet(prev_span).rfind('\n') {
|
|
|
|
Some(offset) => self.last_pos + BytePos(offset as u32),
|
|
|
|
None => source!(self, span).lo,
|
|
|
|
};
|
|
|
|
self.format_missing(span_end);
|
|
|
|
self.last_pos = source!(self, span).hi;
|
|
|
|
}
|
|
|
|
Some(ref s) => {
|
|
|
|
let s = format!("{}use {};", vis, s);
|
|
|
|
self.format_missing_with_indent(source!(self, span).lo);
|
|
|
|
self.buffer.push_str(&s);
|
|
|
|
self.last_pos = source!(self, span).hi;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.format_missing_with_indent(source!(self, span).lo);
|
|
|
|
self.format_missing(source!(self, span).hi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String {
|
|
|
|
let mut item_str = vpi.node.name.to_string();
|
|
|
|
if item_str == "self" {
|
|
|
|
item_str = "".to_owned();
|
|
|
|
}
|
|
|
|
let path_item_str = if path_str.is_empty() {
|
|
|
|
if item_str.is_empty() {
|
|
|
|
"self".to_owned()
|
|
|
|
} else {
|
|
|
|
item_str
|
|
|
|
}
|
|
|
|
} else if item_str.is_empty() {
|
|
|
|
path_str
|
|
|
|
} else {
|
|
|
|
format!("{}::{}", path_str, item_str)
|
2015-09-26 18:12:25 +12:00
|
|
|
};
|
|
|
|
append_alias(path_item_str, vpi)
|
|
|
|
}
|
|
|
|
|
2015-10-04 20:20:15 +02:00
|
|
|
fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option<String> {
|
2016-09-16 15:19:18 +12:00
|
|
|
Some(append_alias(vpi.node.name.to_string(), vpi))
|
2015-09-26 18:12:25 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
fn append_alias(path_item_str: String, vpi: &ast::PathListItem) -> String {
|
2016-09-16 15:19:18 +12:00
|
|
|
match vpi.node.rename {
|
|
|
|
Some(rename) => format!("{} as {}", path_item_str, rename),
|
|
|
|
None => path_item_str,
|
2015-05-01 17:27:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 18:09:05 +02:00
|
|
|
// Pretty prints a multi-item import.
|
|
|
|
// Assumes that path_list.len() > 0.
|
2017-01-31 08:28:48 +13:00
|
|
|
pub fn rewrite_use_list(shape: Shape,
|
2015-07-17 15:58:47 +02:00
|
|
|
path: &ast::Path,
|
|
|
|
path_list: &[ast::PathListItem],
|
|
|
|
span: Span,
|
2015-08-14 14:09:19 +02:00
|
|
|
context: &RewriteContext)
|
2015-07-17 15:58:47 +02:00
|
|
|
-> Option<String> {
|
2016-06-08 13:00:11 +02:00
|
|
|
// Returns a different option to distinguish `::foo` and `foo`
|
2017-01-31 08:28:48 +13:00
|
|
|
let path_str = try_opt!(rewrite_path(context, PathContext::Import, None, path, shape));
|
2015-07-17 15:58:47 +02:00
|
|
|
|
|
|
|
match path_list.len() {
|
2015-09-04 18:09:05 +02:00
|
|
|
0 => unreachable!(),
|
2017-01-19 10:47:07 +13:00
|
|
|
1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
|
2015-08-16 15:58:17 +12:00
|
|
|
_ => (),
|
2015-07-17 15:58:47 +02:00
|
|
|
}
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2017-03-25 22:21:43 -07:00
|
|
|
let colons_offset = if path_str.is_empty() { 0 } else { 2 };
|
|
|
|
|
|
|
|
// 2 = "{}"
|
2017-03-28 11:14:47 +13:00
|
|
|
let remaining_width = shape
|
|
|
|
.width
|
|
|
|
.checked_sub(path_str.len() + 2 + colons_offset)
|
|
|
|
.unwrap_or(0);
|
2015-07-17 15:58:47 +02:00
|
|
|
|
2015-08-19 18:12:05 +02:00
|
|
|
let mut items = {
|
|
|
|
// Dummy value, see explanation below.
|
|
|
|
let mut items = vec![ListItem::from_str("")];
|
|
|
|
let iter = itemize_list(context.codemap,
|
|
|
|
path_list.iter(),
|
|
|
|
"}",
|
|
|
|
|vpi| vpi.span.lo,
|
|
|
|
|vpi| vpi.span.hi,
|
2015-09-26 18:12:25 +12:00
|
|
|
rewrite_path_item,
|
2016-03-07 13:41:32 -05:00
|
|
|
context.codemap.span_after(span, "{"),
|
2015-08-19 18:12:05 +02:00
|
|
|
span.hi);
|
|
|
|
items.extend(iter);
|
|
|
|
items
|
|
|
|
};
|
2015-07-17 15:58:47 +02:00
|
|
|
|
|
|
|
// We prefixed the item list with a dummy value so that we can
|
|
|
|
// potentially move "self" to the front of the vector without touching
|
|
|
|
// the rest of the items.
|
|
|
|
let has_self = move_self_to_front(&mut items);
|
2016-05-29 17:58:38 +02:00
|
|
|
let first_index = if has_self { 0 } else { 1 };
|
2015-05-02 19:12:16 +02:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.reorder_imported_names() {
|
2015-07-17 15:58:47 +02:00
|
|
|
items[1..].sort_by(|a, b| a.item.cmp(&b.item));
|
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
|
2015-10-06 22:13:14 +02:00
|
|
|
let tactic = definitive_tactic(&items[first_index..],
|
|
|
|
::lists::ListTactic::Mixed,
|
|
|
|
remaining_width);
|
2017-03-25 22:21:43 -07:00
|
|
|
|
2015-10-02 13:50:24 +02:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
2017-03-25 22:21:43 -07:00
|
|
|
// Add one to the indent to account for "{"
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape::legacy(remaining_width,
|
2017-03-25 22:21:43 -07:00
|
|
|
shape.indent + path_str.len() + colons_offset + 1),
|
2015-10-02 13:50:24 +02:00
|
|
|
ends_with_newline: false,
|
|
|
|
config: context.config,
|
|
|
|
};
|
2015-09-04 18:09:05 +02:00
|
|
|
let list_str = try_opt!(write_list(&items[first_index..], &fmt));
|
2015-06-26 03:29:54 +02:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
Some(if path_str.is_empty() {
|
2017-02-22 16:20:50 +13:00
|
|
|
format!("{{{}}}", list_str)
|
|
|
|
} else {
|
|
|
|
format!("{}::{{{}}}", path_str, list_str)
|
|
|
|
})
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-06-26 03:29:54 +02:00
|
|
|
|
|
|
|
// Returns true when self item was found.
|
|
|
|
fn move_self_to_front(items: &mut Vec<ListItem>) -> bool {
|
2017-03-28 11:14:47 +13:00
|
|
|
match items
|
|
|
|
.iter()
|
|
|
|
.position(|item| item.item.as_ref().map(|x| &x[..]) == Some("self")) {
|
2015-06-26 03:29:54 +02:00
|
|
|
Some(pos) => {
|
|
|
|
items[0] = items.remove(pos);
|
|
|
|
true
|
2015-08-16 15:58:17 +12:00
|
|
|
}
|
|
|
|
None => false,
|
2015-06-26 03:29:54 +02:00
|
|
|
}
|
|
|
|
}
|