Skip to content

Commit

Permalink
Enable links to locations within comment editors
Browse files Browse the repository at this point in the history
Makes sure that if you write a comment such as:

```
[ref]

[ref]: http://example.com
```

Clicking on `ref` navigates to the definition instead of opening the comment contents in a new editor
  • Loading branch information
mjbvz committed Apr 5, 2024
1 parent bd3b1fe commit a719185
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,44 @@
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IEditorContribution, ScrollType } from 'vs/editor/common/editorCommon';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { ITextModel } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService';
import { ITextResourceEditorInput } from 'vs/platform/editor/common/editor';
import { applyTextEditorOptions } from 'vs/workbench/common/editor/editorOptions';
import { SimpleCommentEditor } from 'vs/workbench/contrib/comments/browser/simpleCommentEditor';

export class CommentsInputContentProvider extends Disposable implements ITextModelContentProvider, IEditorContribution {

public static readonly ID = 'comments.input.contentProvider';

constructor(
@ITextModelService textModelService: ITextModelService,
@ICodeEditorService codeEditorService: ICodeEditorService,
@IModelService private readonly _modelService: IModelService,
@ILanguageService private readonly _languageService: ILanguageService,
) {
super();
this._register(textModelService.registerTextModelContentProvider(Schemas.commentsInput, this));

this._register(codeEditorService.registerCodeEditorOpenHandler(async (input: ITextResourceEditorInput, editor: ICodeEditor | null, _sideBySide?: boolean): Promise<ICodeEditor | null> => {
if (!(editor instanceof SimpleCommentEditor)) {
return null;
}

if (editor.getModel()?.uri.toString() !== input.resource.toString()) {
return null;
}

if (input.options) {
applyTextEditorOptions(input.options, editor, ScrollType.Immediate);
}
return editor;
}));
}

async provideTextContent(resource: URI): Promise<ITextModel | null> {
Expand Down

0 comments on commit a719185

Please sign in to comment.