<<xAct`xCoba`

DefManifold[M4,4,IndexRange[a,f]]

DefMetric[-1,metricg[-a,-b],Cd,{",","∂"},PrintAs->"g"]

Bug 1 - doesn’t recognise sums of covariant derivatives

DefTensor[v[a],M4]

Cd[a]@v[-b]

IndexCoefficient[Cd[a]@v[-b],Cd[-c]@v[d]]

IndexCoefficient[Cd[-b]@v[a],Cd[-c]@v[d]]

IndexCoefficient[(Cd[a]@v[-b]-Cd[-b]@v[a]),Cd[-c]@v[d]]

(Cd[a]@v[-b]-Cd[-b]@v[a])/2

IndexCoefficient[(Cd[a]@v[-b]-Cd[-b]@v[a])/2,Cd[-c]@v[d]]

IndexCoefficient[Expand[(Cd[a]@v[-b]-Cd[-b]@v[a])/2],Cd[-c]@v[d]]//Simplification

Bug 2 - doesn’t recognise scalar products

DefTensor[s[],M4]

IndexCoefficient[4 π s[],s[]]

IndexCoefficient[4 π s[] s[],s[]]

IndexCoefficient[4 π ((s[])^2),s[]]

Leo was here

When acting on a scalar this falls through to the builtin Coefficient function

Trace[IndexCoefficient[4 π ((s[])^2),s[]]]

Coefficient acts differently from how IndexCoefficient acts.
Specifically, we want to have the identity
  tens IndexCoefficient[expr, tens] == expr
which is not true for Coefficient when acting on powers (except for the first power).

So, first we need to clear out the following definition:

Unprotect[IndexCoefficient];
IndexCoefficient[expr_,form_?xAct`xTensor`Private`NonIndexedScalarQ]=.;
Protect[IndexCoefficient];

It still doesn’t work, but now for a different reason:

IndexCoefficient[4 π ((s[])^2),s[]]

Here is a very ugly workaround. Since IndexCoefficient treats products of tensors in the correct way, we turn a scalar into a product of a vector and 1-form, act with IndexCoefficient, then change back

DefTensor[cheat1[a],M4];
DefTensor[cheat2[-a],M4];

$PrePrint=ScreenDollarIndices;

4 π ((s[])^2)/.{s[]->Scalar[cheat1[a]cheat2[-a]]}//NoScalar
IndexCoefficient[%,cheat1[a]cheat2[-a]]//PutScalar
%/.{Scalar[cheat1[a_]cheat2[-a_]]->s[]}