mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
[html] Follow Link in Multi Root Workspace. Fixes #38469
This commit is contained in:
parent
099e284283
commit
2fa4edeaa6
|
@ -8,8 +8,8 @@
|
|||
"node": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-css-languageservice": "^3.0.2",
|
||||
"vscode-html-languageservice": "^2.0.13",
|
||||
"vscode-css-languageservice": "^3.0.3",
|
||||
"vscode-html-languageservice": "^2.0.14",
|
||||
"vscode-languageserver": "^3.5.0",
|
||||
"vscode-nls": "^2.0.2",
|
||||
"vscode-uri": "^1.0.1"
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
'use strict';
|
||||
|
||||
import { createConnection, IConnection, TextDocuments, InitializeParams, InitializeResult, RequestType, DocumentRangeFormattingRequest, Disposable, DocumentSelector, TextDocumentPositionParams, ServerCapabilities, Position } from 'vscode-languageserver';
|
||||
import { DocumentContext } from 'vscode-html-languageservice';
|
||||
import { TextDocument, Diagnostic, DocumentLink, SymbolInformation } from 'vscode-languageserver-types';
|
||||
import { getLanguageModes, LanguageModes, Settings } from './modes/languageModes';
|
||||
|
||||
|
@ -15,13 +14,11 @@ import { DidChangeWorkspaceFoldersNotification, WorkspaceFolder } from 'vscode-l
|
|||
|
||||
import { format } from './modes/formatting';
|
||||
import { pushAll } from './utils/arrays';
|
||||
import { endsWith, startsWith } from './utils/strings';
|
||||
|
||||
import * as url from 'url';
|
||||
import * as path from 'path';
|
||||
import { getDocumentContext } from './utils/documentContext';
|
||||
import uri from 'vscode-uri';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
|
||||
nls.config(process.env['VSCODE_NLS_CONFIG']);
|
||||
|
||||
namespace TagCloseRequest {
|
||||
|
@ -41,7 +38,6 @@ let documents: TextDocuments = new TextDocuments();
|
|||
// for open, change and close text document events
|
||||
documents.listen(connection);
|
||||
|
||||
let workspacePath: string | undefined | null;
|
||||
let workspaceFolders: WorkspaceFolder[] | undefined;
|
||||
|
||||
var languageModes: LanguageModes;
|
||||
|
@ -77,8 +73,13 @@ function getDocumentSettings(textDocument: TextDocument, needsDocumentSettings:
|
|||
connection.onInitialize((params: InitializeParams): InitializeResult => {
|
||||
let initializationOptions = params.initializationOptions;
|
||||
|
||||
workspacePath = params.rootPath;
|
||||
workspaceFolders = (<any>params).workspaceFolders;
|
||||
if (!Array.isArray(workspaceFolders)) {
|
||||
workspaceFolders = [];
|
||||
if (params.rootPath) {
|
||||
workspaceFolders.push({ name: '', uri: uri.file(params.rootPath).toString() });
|
||||
}
|
||||
}
|
||||
|
||||
languageModes = getLanguageModes(initializationOptions ? initializationOptions.embeddedLanguages : { css: true, javascript: true });
|
||||
documents.onDidClose(e => {
|
||||
|
@ -305,45 +306,19 @@ connection.onDocumentRangeFormatting(async formatParams => {
|
|||
|
||||
connection.onDocumentLinks(documentLinkParam => {
|
||||
let document = documents.get(documentLinkParam.textDocument.uri);
|
||||
let documentContext: DocumentContext = {
|
||||
resolveReference: (ref, base) => {
|
||||
if (base) {
|
||||
ref = url.resolve(base, ref);
|
||||
}
|
||||
if (ref[0] === '/') {
|
||||
let root = getRootFolder(document.uri);
|
||||
if (root) {
|
||||
return uri.file(path.join(root, ref)).toString();
|
||||
}
|
||||
}
|
||||
return url.resolve(document.uri, ref);
|
||||
},
|
||||
|
||||
};
|
||||
let links: DocumentLink[] = [];
|
||||
languageModes.getAllModesInDocument(document).forEach(m => {
|
||||
if (m.findDocumentLinks) {
|
||||
pushAll(links, m.findDocumentLinks(document, documentContext));
|
||||
}
|
||||
});
|
||||
if (document) {
|
||||
let documentContext = getDocumentContext(document.uri, workspaceFolders);
|
||||
languageModes.getAllModesInDocument(document).forEach(m => {
|
||||
if (m.findDocumentLinks) {
|
||||
pushAll(links, m.findDocumentLinks(document, documentContext));
|
||||
}
|
||||
});
|
||||
}
|
||||
return links;
|
||||
});
|
||||
|
||||
function getRootFolder(docUri: string): string | undefined {
|
||||
if (workspaceFolders) {
|
||||
for (let folder of workspaceFolders) {
|
||||
let folderURI = folder.uri;
|
||||
if (!endsWith(folderURI, '/')) {
|
||||
folderURI = folderURI + '/';
|
||||
}
|
||||
if (startsWith(docUri, folderURI)) {
|
||||
return uri.parse(folderURI).fsPath;
|
||||
}
|
||||
}
|
||||
return void 0;
|
||||
}
|
||||
return workspacePath;
|
||||
}
|
||||
|
||||
|
||||
connection.onDocumentSymbol(documentSymbolParms => {
|
||||
let document = documents.get(documentSymbolParms.textDocument.uri);
|
||||
|
|
22
extensions/html/server/src/test/documentContext.test.ts
Normal file
22
extensions/html/server/src/test/documentContext.test.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { getDocumentContext } from '../utils/documentContext';
|
||||
|
||||
suite('Document Context', () => {
|
||||
|
||||
test('Context', function (): any {
|
||||
const docURI = 'file:///users/test/folder/test.html';
|
||||
const rootFolders = [{ name: '', uri: 'file:///users/test/' }];
|
||||
|
||||
let context = getDocumentContext(docURI, rootFolders);
|
||||
assert.equal(context.resolveReference('/', docURI), 'file:///users/test/');
|
||||
assert.equal(context.resolveReference('/message.html', docURI), 'file:///users/test/message.html');
|
||||
assert.equal(context.resolveReference('message.html', docURI), 'file:///users/test/folder/message.html');
|
||||
assert.equal(context.resolveReference('message.html', 'file:///users/test/'), 'file:///users/test/message.html');
|
||||
});
|
||||
});
|
40
extensions/html/server/src/utils/documentContext.ts
Normal file
40
extensions/html/server/src/utils/documentContext.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { DocumentContext } from 'vscode-html-languageservice';
|
||||
import { endsWith, startsWith } from '../utils/strings';
|
||||
import * as url from 'url';
|
||||
import { WorkspaceFolder } from 'vscode-languageserver-protocol/lib/protocol.workspaceFolders.proposed';
|
||||
|
||||
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
|
||||
function getRootFolder(): string | undefined {
|
||||
for (let folder of workspaceFolders) {
|
||||
let folderURI = folder.uri;
|
||||
if (!endsWith(folderURI, '/')) {
|
||||
folderURI = folderURI + '/';
|
||||
}
|
||||
if (startsWith(documentUri, folderURI)) {
|
||||
return folderURI;
|
||||
}
|
||||
}
|
||||
return void 0;
|
||||
}
|
||||
|
||||
return {
|
||||
resolveReference: (ref, base = documentUri) => {
|
||||
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
|
||||
if (startsWith(base, 'file://')) {
|
||||
let folderUri = getRootFolder();
|
||||
if (folderUri) {
|
||||
return folderUri + ref.substr(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return url.resolve(base, ref);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -10,16 +10,16 @@
|
|||
version "7.0.43"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.43.tgz#a187e08495a075f200ca946079c914e1a5fe962c"
|
||||
|
||||
vscode-css-languageservice@^3.0.2:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-3.0.2.tgz#ae0c43836318455aa290c777556394d6127b8f6c"
|
||||
vscode-css-languageservice@^3.0.3:
|
||||
version "3.0.3"
|
||||
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-3.0.3.tgz#02cc4efa5335f5104e0a2f3b6920faaf59db4f7a"
|
||||
dependencies:
|
||||
vscode-languageserver-types "3.5.0"
|
||||
vscode-nls "^2.0.1"
|
||||
|
||||
vscode-html-languageservice@^2.0.13:
|
||||
version "2.0.13"
|
||||
resolved "https://registry.yarnpkg.com/vscode-html-languageservice/-/vscode-html-languageservice-2.0.13.tgz#09c4437cffb0800b865d71552f4dfd8240c796d8"
|
||||
vscode-html-languageservice@^2.0.14:
|
||||
version "2.0.14"
|
||||
resolved "https://registry.yarnpkg.com/vscode-html-languageservice/-/vscode-html-languageservice-2.0.14.tgz#15491c11bb7e196f6fd03b6c6c768901935862eb"
|
||||
dependencies:
|
||||
vscode-languageserver-types "3.5.0"
|
||||
vscode-nls "^2.0.2"
|
||||
|
|
Loading…
Reference in a new issue