Repository: GraphiteEditor/Graphite
File: editor/src/messages/tool/common_functionality/shape_editor.rs (Rust)

Code before:
				// Get the local positions of the selected points
				let start_local_pos = document.network_interface.compute_modified_vector(layer1).and_then(|v| v.point_domain.position_from_id(start_point));
				let end_local_pos = document.network_interface.compute_modified_vector(layer2).and_then(|v| v.point_domain.position_from_id(end_point));

				// Transform to document/world space
				let start_transform = document.metadata().transform_to_document(layer1);
				let end_transform = document.metadata().transform_to_document(layer2);

				if let (Some(start_local), Some(end_local)) = (start_local_pos, end_local_pos) {
					let start_pos = start_transform.transform_point2(start_local);
					let end_pos = end_transform.transform_point2(end_local);

					merge_layers(document, layer1, layer2, responses);

					responses.add(DeferMessage::AfterGraphRun {
						messages: vec![
							ToolMessage::Path(PathToolMessage::ConnectPointsByPosition {
								layer: layer1,
								start_position: start_pos,
								end_position: end_pos,
							})
							.into(),
						],
					});
				}
			}
			return;
		}

		// If no points are selected, try to find a single continuous subpath in each layer to connect the endpoints of
		for &layer in self.selected_shape_state.keys() {
			let Some(vector) = document.network_interface.compute_modified_vector(layer) else { continue };

			let endpoints: Vec<PointId> = vector.point_domain.ids().iter().copied().filter(|&point_id| vector.all_connected(point_id).count() == 1).collect();

			if endpoints.len() == 2 {
				let start_point = endpoints[0];
				let end_point = endpoints[1];

				let segment_id = SegmentId::generate();
				let modification_type = VectorModificationType::InsertSegment {
					id: segment_id,
					points: [end_point, start_point],
					handles: [None, None],
				};
				responses.add(GraphOperationMessage::Vector { layer, modification_type });
			}
		}
	}

	// Snap, returning a viewport delta

Review comment (refactor):
This is basically duplicated from the `handle_grouped_transform_close_path` function.

Code after:
				if is_grouped {
					// Grouped/merged layer: use helper function to handle reorganization
					Self::handle_grouped_transform_close_path(document, layer1, start_point, end_point, responses);
				} else {
					// Single segment: PointIDs are stable, use immediate insertion
					let segment_id = SegmentId::generate();
					let modification_type = VectorModificationType::InsertSegment {
						id: segment_id,
						points: [end_point, start_point],
						handles: [None, None],
					};
					responses.add(GraphOperationMessage::Vector { layer: layer1, modification_type });
				}
			} else {
				// Different layers: merge first, then create segment
				merge_layers(document, layer1, layer2, responses);

				// Use the helper to defer the connection until after reorganization
				Self::defer_connect_points_by_position(document, layer1, start_point, layer2, end_point, layer1, responses);
			}
			return;
		}

		// If no points are selected, try to find a single continuous subpath in each layer to connect the endpoints of
		for &layer in self.selected_shape_state.keys() {
			let Some(vector) = document.network_interface.compute_modified_vector(layer) else { continue };

			let endpoints: Vec<PointId> = vector.point_domain.ids().iter().copied().filter(|&point_id| vector.all_connected(point_id).count() == 1).collect();

			if endpoints.len() == 2 {
				let start_point = endpoints[0];
				let end_point = endpoints[1];

				let segment_id = SegmentId::generate();
				let modification_type = VectorModificationType::InsertSegment {
					id: segment_id,
					points: [end_point, start_point],
					handles: [None, None],
				};
				responses.add(GraphOperationMessage::Vector { layer, modification_type });
			}
		}
	}

	// Snap, returning a viewport delta
	pub fn snap(
		&self,
		snap_manager: &mut SnapManager,
		snap_cache: &SnapCache,
		document: &DocumentMessageHandler,
		input: &InputPreprocessorMessageHandler,