First-principles theory of spatial dispersion